UITableViewCell delete button does not disappear

I use UISegmentedControl to switch a UITableView between two datasets (think favorites and duplicates). Pressing a segmented control reloads the table view with another dataset.

 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:anim]; 

When a user deletes a row, it works fine. HOWEVER , when a user switches datasets through a segmented control, DELETED CELL is reused without changing its appearance (ie the red DELETE button still exists, and the contents of the row are nowhere to be seen), This seems to be the opposite problem. which most people see that the delete button does not appear.

This is the removal code:

 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { if ([self.current isEqualTo:self.favorites]) { Favorite *fav = self.favorites[indexPath.row]; NSMutableArray *mut = [self.favorites mutableCopy]; [mut removeObjectAtIndex:indexPath.row]; self.favorites = mut; self.current = self.favorites; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } } } 

The tableview has a single selection and self.tableView.editing == NO . I also tried to use [self.tableView reloadData] and remove / insert the difference in rows from one data set to the next. Nothing works.

UITableViewCell I use no backgroundView or selectedBackgroundView prefixes


[EDIT]

Changed the value of segmented control:

 - (IBAction)modeChanged:(id)sender { if (self.listMode.selectedSegmentIndex == 1) { self.current = self.favorites; } else { self.current = self.recents; } // Tryin this: [self.tableView reloadData]; // Tried this: // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; } // Only 1 Section per table - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return [self.current count]; } 
+7
ios uitableview ios7
source share
2 answers

I came across the same thing: โ€œdeleteโ€ a custom UITableViewCell, I deleted it from the table and put it in another list, which the user could display in a modal form when they regret and want to put it back. In iOS7 (but not iOS6), such displaced cells had a large ugly DELETE button, despite the call to setEditing: NO, etc. (And besides, the rest of the cell contents were not drawn at all, although checking the cells in the debugger showed that all subfolders still exist.)

Unlike Stephen above, I did not override prepareForReuse, so this is not a problem. But it was connected: in my case, the cells were not created with the reuse identifier:

 self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 

And in the docs: "If the cell object does not have an associated reuse identifier, this method is not called." But apparently, at least in iOS7 it should be.

So the solution in my case was to explicitly call this [cell prepareForReuse] in each cell when I loaded it into a new table.

+1
source share

Oh, for the sake of love ...

I did not call [super prepareForReuse]; in my subclass of UITableViewCell .

Ugh.

+21
source share

All Articles