In mine UITableView, when it enters edit mode, I would like to select only a few selected cells. I know that a class UITableViewhas a property allowsSelectionDuringEditing, but this applies to everything UITableView. I do not see the appropriate delegate methods to set this for each cell.
The best solution I can come up with is install allowsSelectionDuringEditingin YES. Then, in didSelectRowAtIndexPath, filter out any unwanted choices if editing the table. Also, in cellForRowAtIndexPathchange these cells selectionStyleto None.
The problem is that switching to edit mode does not restart UITableViewCells, so they selectionStyledo not change until they scroll the screen. So, in setEditing, I also need to iterate over the visible cells and set them selectionStyle.
This works, but I'm just wondering if there is a better / more elegant solution to this problem. The main outline of my code is attached. Any suggestions are greatly appreciated! Thank.
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (self.editing && ![self _isUtilityRow:indexPath]) return;
}
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if (self.editing && ![self _isUtilityRow:indexPath])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
return cell;
}
- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing)
{
for (UITableViewCell* cell in [self.tableView visibleCells])
{
if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
}
else
{
for (UITableViewCell* cell in [self.tableView visibleCells])
{
if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
}
}
}
source
share