Select a program table cell: selectRowAtIndexPath vs setSelected

I often need to set table view cells to their original selected state, for which I use the following code:

[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; [cell setSelected:YES]; cell.accessoryType = UITableViewCellAccessoryCheckmark; 

I am using selectRowAtIndexPath:indexPath and setSelected:YES at the same time because I do not quite understand which of the two methods is the preferred way to programmatically select the cell.

Which statement should be used and why?

+7
source share
1 answer

I believe that the method you want to use is selectRowAtIndexPath:animated:scrollPosition: Generally, you should leave cell state control in the table view. If selected, it saves and maintains a set of selected pointer routes, so the correct line will remain selected after reusing another cell. There is also no need to call both methods, it is just superfluous.

+6
source

All Articles