UITableViewCell: how to verify the kind of accessoryType in all cells?

Posted by R31n4ld0_ on Stack Overflow See other posts from Stack Overflow or by R31n4ld0_
Published on 2010-03-16T22:20:19Z Indexed on 2010/03/16 22:21 UTC
Read the original article Hit count: 188

Hello, Guys.

I have a UITableView in that some cells are marked with UITableViewCellAccessoryCheckmark at the initialization of the view.

When the user selects another row, I have to check if the maximum number of selected rows was achieved before. To do that, I used the code bellow:

- (NSInteger)tableView:(UITableView *)tableView numberOfSelectedRowsInSection:(NSInteger)section{

 NSInteger numberOfRows         = [self tableView:tableView numberOfRowsInSection:section];
 NSInteger numberOfSelectedRows = 0;

 for (int i = 0; i < numberOfRows; i++) {

  UITableViewCell *otherCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]];

  if (otherCell.accessoryType == UITableViewCellAccessoryCheckmark) {
   numberOfSelectedRows++;
  }  
 }

 return numberOfSelectedRows;

}

If my number of rows is, as example, 20, the variable numberOfRows is setted correctly with 20. Lets say that 13 rows already are marked with UITableViewCellAccessoryCheckmark. So, numberOfSelectedRows should be 13 after the loop, but only the marked and VISIBLE cells are considered. So, if I have 9 cells showed and 7 are marked, the numberOfSelectedRows returns 7 instead of 13 (but the for iterate 20 times, as expected).

Is this a correct behavior of UITableView or it is a bug of iPhone simulator?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about uitableview

Related posts about uitableviewcell