I've run into an issue while implementing a UITableView
in an iOS app I'm working on(iOS SDK 4.2). If I tap a cell in the Table View, and then scroll the view so that the cell leaves the screen, when it comes back into view the most recently selected cell has been reselected.
To test this I created a new View Based Application project, dragged out a UITableView
in Interface Builder, and set the view controller as the table view's data source and delegate, and put the following code in the view controller:
- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section {
return 12;
}
- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *cellID = @"aCellID";
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!aCell) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
aCell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row +1];
return aCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
[aCell setSelected:NO animated:YES];
}
When running this(in simulator or on device) if I were to tap any cell(cell 12 for example), it would be selected and deselected. After this, with no cells in the table currently selected, if I scroll the view so that the most recently selected cell(12 in this case) leaves the screen, it will be selected again when it reappears.
Is this the intended default behavior for UITableView
? Or possibly an error in my implementation? What would be best way to deselect this cell in this situation?
It took me asking to figure this one out myself:
My problem was using the UITableViewCell
method setSelected:
instead of using the UITableView
method deselectRowAtIndexPath:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With