在单个 UITableCell 触摸上应用多个 UITableViewCellAccessoryCheckmark 的问题 [英] Issue with multiple UITableViewCellAccessoryCheckmark applied on single UITableCell touch
问题描述
所以我的 UITableView 中有很多单元格,并且在触摸单元格时,刚好在视野之外的其他单元格也应用了刻度.我相信这是因为单元格被重用或由于 dequeueReusableCellWithIdentifier:
so I have a lot of cells in my UITableView and on touching a cell, additional cells that are just out of view also have ticks applied. I believe this is because the cells are being reused or something due to dequeueReusableCellWithIdentifier:
这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone){
[addPapersSelectedRowsArray addObject:[NSNumber numberWithInt:indexPath.row]];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
[addPapersSelectedRowsArray removeObject:[NSNumber numberWithInt:indexPath.row]];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *currentPapersSecltion = [papersDataDictionary objectAtIndex:indexPath.row];
[[cell textLabel] setText:[currentPapersSecltion objectForKey:@"Title"]];
return cell;
}
也在 viewDidLoad
中从 .plist 中提取数据:
Also pulling data from a .plist in viewDidLoad
:
//Load papers data from the local plist
papersDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"papers.plist"]] objectForKey:@"Rows"];
有谁知道如何解决这个问题?谢谢各位!
Does anyone know how to fix this? Thanks guys!
德克斯
推荐答案
在 cellForRowAtIndexPath 中需要根据单元格是否被选中来设置复选标记.
In cellForRowAtIndexPath which you need to set your checkmark according to whether or not the cell has been selected.
if ([addPapersSelectedRowsArray containsObject:[NSNumber numberWithInt:[indexPath row]]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
这篇关于在单个 UITableCell 触摸上应用多个 UITableViewCellAccessoryCheckmark 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!