The ChatViewController of acaniChat has two different types of cells with cell identifiers: @"MessageCellID" and @"TimestampCellID". How do I make it so that only the message cells are editable?
Here’s what I did. It kinda works, but only the message cells on screen are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return [[chatContent cellForRowAtIndexPath:indexPath] reuseIdentifier] == MessageCellId;
}
This works for all cells, but is it the correct practice?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return [[cellMap objectAtIndex:[indexPath row]] isKindOfClass:[Message class]];
}
My “answer” is more of a recommendation that you use a data source check vs a cell check to determine whether the cell is editable. The
tableView:canEditRowAtIndexPath:is a UITableViewDataSource protocol message. The reason it’s part of the data source is b/c generally it is data model logic that determines whether or not the data in a particular cell should be editable. In both of your examples, you are using the type of cell as a proxy for the type of data in the cell.A better check would be similar to the logic you must be using in
tableView:cellForRowAtIndexPath:to determine which kind of cell to use.Without knowing the specifics of the data model I can’t give you the exact code but it’s something like: