Wrong Editing Controls Displayed in UITableView
I'm having a strange problem with UITableView. When the user taps the Edit button, the tableview (which is a grouped view with multiple sections) is supposed to show delete buttons for each row--except for the final row in each section, which has a green add button.
开发者_开发百科When a user taps the green button, a new row is inserted, but now the final row gets a delete button. Even stranger, that delete button ACTS like an add button. So it seems there's a drawing glitch, rather than a problem in assigning the correct style. (Extensive NSLogging shows that the last cell is getting the Insert editing style correctly.)
I've tried setting setNeedsDisplay on the cell and the tableView, I've tried reloading that section/row/the entire table, but the issue persists. Any ideas on how to get UITableView to explicitly redraw the editing controls?
I had the same problem. It seems like the cell's gets reused too much..
You could fix it by enforcing new cells for the inserted cells by using different identifiers:
if([tableView isEditing] && indexPath.row == [items count]){
static NSString *CellIdentifier = @"AddCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// "add-cell" setup here.. e.g"
[[cell textLabel] setText:@"Tab to add new item"];
return cell;
}else{
static NSString *CellIdentifier = @"NormalCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// normal cell setup here..
return cell;
}
I'm pretty sure you can just implement this in your UITableView delegate to prevent it from editing the last row:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
if (indexPath.row != lastRow) // whatever your last row is
return YES;
}
return NO;
}
精彩评论