在阅读了与此相关的几个问题后,我可以使用该功能。但是我的列表很长且可滚动,所以当我向上和向下滚动时,选中的复选标记就到处都是。 selectedIndexes是NSMutableArray
。有什么想法吗?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
在我cellForRowAtIndexPath:
我有以下检查。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSDictionary *dictionary = [natures objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Name"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
for (int i = 0; i < selectedIndexes.count; i++) {
NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];
if (num == indexPath.row) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
return cell;
}
答案 0 :(得分:21)
您需要viewDidLoad
self.tableView.allowsMultipleSelection = YES;
和
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = NO;
// if you don't use a custom image then tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = YES;
// if you don't use a custom image then tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}
答案 1 :(得分:15)
您需要将cellForRowAtIndexPath
修改为以下内容:
// Assume cell is not checked -- if it is the loop below will check it.
[cell setAccessoryType:UITableViewCellAccessoryNone];
for (int i = 0; i < selectedIndexes.count; i++) {
NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];
if (num == indexPath.row) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
// Once we find a match there is no point continuing the loop
break;
}
}
您需要执行此操作,因为可重复使用的单元格可能设置了复选标记 - 因此您需要清除它。希望这会有所帮助。
顺便说一句,使用NSSet
可能是更优雅的解决方案!
答案 2 :(得分:1)
无需维护用于存储所选行的单独数组。
使用本机方法indexPathsForSelectedRows
来获取选定的单元indexPath数组。只需在cellForRow方法中检查arrayContains当前的IndexPath,如下所示:
NSArray *indexPaths = [tableView indexPathsForSelectedRows];
if ([indexPaths containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
#pragma mark- select/deselect
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}