1

I have been scratching my head for a while on this one. I couldn't find any solution so far, so I decided to create a question. I appreciate the help.

My problem:

My table view header gets into delete mode with no value, when I try to delete any arbitrary cell in my table view and gets removed along with the cell that I am deleting. I am not using a reusable cell and I am not sure what causing this problem.

This is what the table view header currently looks like: enter image description here And This is what I want to achieve: enter image description here As for the code:

This is how I am setting my table view header:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.containerTableView.allowsMultipleSelectionDuringEditing = NO;
    if (self.isCorrectDataType)
    {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
        cell.backgroundColor = [UIColor blackColor];
        self.containerTableView.tableHeaderView = cell;
    }
}

This is how I am enabling the editing mode and deleting a cell:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.data count] > 0 ? YES : NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {        
        [self.data removeObjectAtIndex:indexPath.row];

        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.data];
        [self.defaults setObject:data forKey:@"userContent"];
        [self.defaults synchronize];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
Matt
  • 674
  • 11
  • 25
  • Have you tried just using a UIView for your table header? I don't think there is any reason to use a UITableViewCell. – Angela Aug 05 '15 at 20:46
  • @Angela, Yes I tried it with uiview as well. In the uiview version, this problem does not occur but then what I want to achieve is to be able to delete the table header view just like a regular cell. I guess i should just use a regular cell for it. – Matt Aug 05 '15 at 21:07
  • Ahh, that wasn't clear from your question. I think you're right and your best bet is just to use a regular cell instead of a header. I don't think that behavior is supported at all. Another option would be to implement your own swipe to delete behaviour on the header view. – Angela Aug 05 '15 at 21:16

1 Answers1

1

You should not use UITableCell for the custom header. Try using a UIView for that. See the example in this answer: Customize UITableView header section

Community
  • 1
  • 1
dorian
  • 847
  • 5
  • 11