I want to add a section header to my tableview when it is in editing mode. Basically I would just want it to be a part of the datasource to have the same look as the rest of the table (see below image for wanted result). But inserting an object ("Add Contact") to the data source leads to alot of micro management when switching in and out from editing mode and it's actually not part of the data source it is more as a header.
I tried using the following code snippet to achieve the same effect but it didn't turn out right (just added the Add Contact text to sit on top of the section, but not part of it as a grouped table cell).
Anyone have any clues on what I'm missing?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
if (section == 1) {
UITableViewCell *addContactCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
addContactCell.textLabel.text = @"Add Contact";
addContactCell.textLabel.opaque = NO;
return addContactCell;
} else {
return nil;
}
}
You don't need to add the 'Add Contact' row to the datasource. You just have to lie in your numberOfRowsInSection, and in cellForIndexPath: methods.
tableView:numberOfRowsInSection: needs to return the number of rows + 1.
tableView:cellForIndexPath: returns your insert cell if the row number is 0, and otherwise returns the cell for the data for indexPath.row - 1.
You'd have to have a little extra stuff in didSelect etc, but there shouldn't be a lot of micromanagement involved.