UITableViewCell resets when scrolling on screen

I had a problem while implementing UITableView in the iOS app I'm working on (iOS SDK 4.2). If I touch a cell in a table view, and then scroll the view so that the cell leaves the screen when it returns to viewing, the last selected cell was re-selected.

To test this, I created a new application-based application project, pulled out a UITableView in Interface Builder and installed the view controller as a data source and table view delegate and placed 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]; } 

At startup (in the simulator or on the device), if I have to click on any cell (for example, cell 12), it will be selected and canceled. After that, if there are no cells in the currently selected table, if I scroll the view so that the last selected cell (12 in this case) leaves the screen, it will be selected again when it appears again.

Is this the intended default behavior for a UITableView ? Or maybe a mistake in my implementation? What would be the best way to deselect this cell in this situation?

+8
ios cocoa-touch uitableview uiscrollview
source share
1 answer

I needed to tell about myself:

My problem was using the UITableViewCell setSelected: method instead of using the UITableView deselectRowAtIndexPath: method.

+13
source share

All Articles