I am writing a UITableView containing 2 sections. When the table first loads all the cells, it displays the correct information, but when I start scrolling up and down, the detailTextLabel and accessoriesType cells are not updated correctly, so some cells that should contain only a detailed TextLabel also contain an accessory and cells that should contain only the accessory, also contain a detailed TextLabel.
Inside cellForRowAtIndexPath: I use nested switch / case statements to apply the correct values ββto the cells in their corresponding section / row. As far as I can tell the logic in these statements is correct, is it possible that the value of the cell variable is inccorect when updating?
The table loads correctly, but after scrolling the accessory, Type and detailTextLabel are mixed up.
Click for links to table screenshots.
Here is the code inside my subclass of UITableViewController:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [sectionNames count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSArray *headingsSection = [cellTitles objectAtIndex:section]; return [headingsSection count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [sectionNames objectAtIndex:section]; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { self.tableView.allowsSelection = YES; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; cell.textLabel.textColor = [UIColor blackColor]; cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; switch (indexPath.section) { case 0: cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]]; break; case 1: switch (indexPath.row) { case 0: cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; break; case 1: cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; break; case 2: if (defaultAssistOn) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } break; } break; } return cell; }
source share