Используйте переменную экземпляра NSMutableIndexSet
и заполните ее индексом проверяемых ячеек.
Затем в методе cellForRowAtIndexPath
установите тип аксессуара для ячейки UITableViewCellAccessoryTypeCheckmark
или * 1006.* в зависимости от того, что indexPath.row находится в NSMutableIndexSet or not.
Наконец, когда ячейка коснулась, добавьте indexPath.row в набор индексов, если он не прочитан, или удалите его, если он уже присутствовал, чтобыпереключите состояние соответствующей ячейки, затем вызовите reloadData
для tableView.
Я также вижу в вашем коде, что вы не знакомы с механизмом повторного использования UITableViewCells.Вам следует прочитать «Руководство по программированию табличного представления» в документации Apple и узнать, как реализовать cellForRowAtIndexPath более эффективным и реактивным способом (с точки зрения реактивности и объема памяти)
Пример
// Let selectedCellIndexes be an instance variable in your .h of type NSMutableIndexSet*
// Initialize it (probably at the same place you initialise your texts & icons, once for all, probably in your init method
selectedCellIndexes = [[NSMutableIndexSet alloc] init];
Затем для заполнения ячеек:
-(UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// Try to recycle and already allocated cell (but not used anymore so we can reuse it)
UITableViewCell* cell = [tv dequeueCellWithReuseIdentifier:...];
if (cell == nil) {
// If we didn't manage to get a reusable (existing) cell to recycle it
// then allocate a new one and configure its general properties common to all cells
cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease];
// ... configure stuff that are common to all your cells : lineBreakMode, numberOfLines, font... once for all
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
// Then here change the stuff that are different between each cell
// (this code will be executed if the cell has just been allocated as well as if the cell is an old cell being recycled)
cell.textLabel.text = [arryTableIconsText objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[arryTableIcons objectAtIndex:indexPath.row]];
cell.accessoryType = [selectedCellIndexes containsIndex:indexPath.row] ? UITableViewCellAccessoryTypeCheckmark : UITableViewCellAccessoryTypeNone;
return cell;
}
И, наконец, для переключения галочек:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([selectedCellIndexes containsIndex:indexPath.row]) {
[selectedCellIndexes removeIndex:indexPath.row];
} else {
[selectedCellIndexes addIndex:indexPath.row];
}
[tableView reloadData];
}