My UITable reuses cells when it shouldn't!

I have a UITableViewController that has two sections. The first section shows a single cell with centered text, saying Add a new Slide . The second section shows the current slides.

When the user clicks on the Add a new Slide cell, the new UITableVeiwController is pushed onto the stack in which the editor is displayed. If the user saves a new slide (by clicking "Save"), the cell is added to the data source, and the editor is unloaded from the stack.

I have two problems:

  • When the editor is unloaded, if the cell was deleted before Add a new Slide was clicked, the old cell will be displayed instead of the new one. If you click on the UITableViewController button (by clicking the button with an automatically generated back button), this is a fix, but I would like that to not happen at all. (The popup table was not initially updated after the editor appeared, so I added [self.tableView reloadData]; to the viewDidAppear method.)

  • After a certain number of slides, the last slide in the list becomes the Add a new Slide cell. I know that the data is entered correctly, because the other part of the application using the same data source is updated correctly. The table supports editing in the second section, and when you change the order of the cells, it behaves correctly behind the scenes, but the wrong cell still exists.

What could be?

Here are some of my codes:

Please note that when I was about to publish my code, I noticed the mismatch of the braces. Checking for cell==nil seems to cover the second part of the code, which determines the contents of the cells. This captures the cell label in the second part of the table, but the style is still wrong. Since then I fixed the code, but the original is posted here.

 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { if ([indexPath section] == 0 ) { cell = [[[MBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }else if([indexPath section] == 1){ cell = [[[MBTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } if ([indexPath section] == 0) { [cell.textLabel setTextAlignment:UITextAlignmentCenter]; [cell.textLabel setText:@"Add a New Slide"]; }else if([indexPath section] == 1){ NSArray *storedViewsInfo = [[NSArray alloc] initWithArray:[kSettings arrayForKey:@"views"]]; if ([[[storedViewsInfo objectAtIndex:[indexPath row]] valueForKey:@"type"] isEqualToString:@"announcement"]) { [cell.detailTextLabel setText:@"Custom Announcement"]; [cell.textLabel setText:[[[storedViewsInfo objectAtIndex:[indexPath row]] valueForKey:@"info"] valueForKey:@"text"]]; } [storedViewsInfo release]; [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; } } return cell; } 
+2
source share
1 answer

Without looking at the code, the first thing that comes to mind is to check whether you have specified your own cells in different methods of your method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; ?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier1 = @"CellIdentifier1"; static NSString *Cellidentifier2 = @"CellIdentifier2"; if (indexPath.section == kAddSlideSection) { CustomCell *cellType1 = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; ... } else { CustomCell *cellType2 = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; ... } } 

It is also worth considering introducing the delegate method, which is called when your user finishes adding a new slide β€” that is, if the [self.tableview reloadData] call from this method is viewWillAppear instead of viewWillAppear .

+3
source

All Articles