UITableView disables selection between cell selections

When using a UITableView with selection enabled, I can select a row and it will become visible with highlighting highlighted. However, when I select the second row, this happens by default:

  • Line 1 is already selected and markedly highlighted.
  • I finger down line number 2.
  • When my finger is still pressed, both lines # 1 and # 2 are markedly highlighted.
  • Dropping a finger now selects line number 2, and only it is clearly highlighted.

What I'm trying to do is do it in step 3 above, both cells are not selected at the same time. Can this be done?

+4
source share
3 answers

It works

 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{ [self.tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath]; return NO; 

}

0
source

Follow these steps.

  • Select table view in storyboard
  • Select "Single selection" instead of "multiple selection" in the "selection" type (in the attribute inspector)

Hope this helps you.

0
source

OK, I edited this answer based on the discussion.

Assuming you subclass UITableViewCell, use this code in your implementation:

(e.g. CustomTableCell.m)

 #define MyTableCellHighlightedNotification @"MyTableCellHighlighted" - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ // Your custom initialization here [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableCellHighlighted:) name:MyTableCellHighlightedNotification object:nil]; } } - (void) dealloc { [[NSNotifcationCenter defaultCenter] removeObserver:self]; // ...Release ivars... [super dealloc] } - (void) setHighlighted:(BOOL) highlighted { // Default behaviour (defer to super) [super setHighlighted:highlighted]; if(highlighted == YES){ // De-highlight all other cells [[NSNotificationCenter defaultCenter] postNotificationName:MyTableCellHighlightedNotification object:self] } } - (void)tableCellHighlighted:(NSNotification*) notification { // All cells receive this notification if([notifcation object] != self){ // All cells except the notification sender de-highlight themselves [self setHighlighted:NO]; } } 
-1
source

All Articles