好的,我正在慢慢弄清楚。我还有一个问题。我正在使用一个字符串,并表示如果该字符串等于单元格文本,则在加载 tableView 时在其上打勾。
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([cell.textLabel.text isEqualToString:transferData]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
然后我告诉它删除该复选标记并在被选中时相应地添加复选标记:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
cell.accessoryType = UITableViewCellAccessoryNone;
UITableViewCell *cellCheck = [tableView
cellForRowAtIndexPath:indexPath];
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
transferData = cellCheck.textLabel.text;
NSLog(@"%@", transferData);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:indexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
一切正常,除了首次加载时。出于某种原因,当我选择另一个单元格时,最初加载 tableView 的复选标记不会消失。这是为什么?
最佳答案
你犯了一个常见的错误。
选择单元格时,您直接设置复选标记的状态。您应该做的是在数据源中设置复选标记的状态,并让表格单元格从数据源进行 self 配置。
排他检查 TableView 的编辑示例
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *changedIndexPaths = nil;
NSIndexPath *currentCheckedIndexPath = [self indexPathOfCurrentCheckedObject];
if (currentCheckedIndexPath && ![currentCheckedIndexPath isEqual:indexPath]) {
// There is currently a checked index path - unselect the data source and
// add it to the changed index array.
[[self.tableData objectAtIndex:currentCheckedIndexPath.row] setChecked:NO];
changedIndexPaths = @[indexPath, currentCheckedIndexPath];
} else{
changedIndexPaths = @[indexPath];
}
[[self.tableData objectAtIndex:indexPath.row] setChecked:YES];
[self.tableView reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationNone];
}
我有一个新的 sample app您可以下载以查看整个项目:
关于objective-c - 为什么 UITableViewCell 附件复选标记不会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13215073/