I’ve been struggling with this problem for a good 2 hours now and I can not get it to work.
I have a view controller with a table view on it with a list of items. When the user touches the EDIT button in the navigation bar the table view should enter “edit mode” and under the list of items one new cell should appear that should only be visible when it is in “edit mode”.
So in my -tableView:cellForRowAtIndexPath: method I’m checking if (tableView.isEditing) to setup the cell. I’ve tried overriding -setEditing:animated: and doing [tableView reloadData] calls but I just can’t get it to work.
This is what my -setEditing:animated: look like:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.groupDetailsTableView setEditing:editing animated:animated];
}
My -tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (tableView.isEditing) {
// Other tableview code has been omitted
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
// Set the background view of the cell to be transparent
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setFrame:[cell.contentView frame]];
[deleteButton setFrame:CGRectMake(0, 0, cell.bounds.size.width-20, 44)];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"redbutton.png"] forState:UIControlStateNormal];
[deleteButton setTitle:@"Delete Group" forState:UIControlStateNormal];
[deleteButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[deleteButton.titleLabel setShadowColor:[UIColor lightGrayColor]];
[deleteButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[deleteButton addTarget:self action:@selector(deleteGroup) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteButton];
return cell;
}
}
And I’m calling [self setEditing:YES animated:YES]; from a method when the user touches the Edit button. I’ve also tried just calling [super setEditing:YES animated:YES]; and [self.groupDetailsTableView setEditing:YES animated:YES];
Found the answer myself at http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19
Apparently
-tableView:cellForRowAtIndexPath:doesn’t get called automatically when calling-setEditing:animated:So to have a new cell “appear” I set the alpha to 0.0 and then 1.0 after calling setEditing…