<分区>
<分区>
我正在开发一个 iOS 应用程序,用户可以在其中选择自己喜欢的景点。我想在 UITableView
中用复选标记显示它。调试时发现,当用户选择(例如)4个景点并向下滚动时,其他景点也会有一个勾号。即使用户没有选择它们。这是一个常见的错误/故障吗?
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = items[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *tableCell = [tableView cellForRowAtIndexPath:indexPath];
BOOL isSelected = (tableCell.accessoryType == UITableViewCellAccessoryCheckmark);
if (isSelected) {
tableCell.accessoryType = UITableViewCellAccessoryNone;
}
else {
tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
`
这是一个 .gif file向您展示朗姆酒时间发生的事情:
最佳答案
这是一个非常常见的错误,但不是 iOS 错误。 :)
问题是单元格会被重复使用,如果您更改单元格的某个方面,当它再次提供给您时,它不会自动重置为任何默认值。
使用的策略是:在用于填充单元格的数据中保存“选定”状态,然后在 cellForRowAtIndexPath
中打开或关闭附件。
关于ios - UITableView 行选择故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33999456/
相关文章:
objective-c - NSDateFormatter dateFromString 返回不正确的日期
c++ - 在 Xcode 4 中构建用于 Objective-c 应用程序的静态 C++ 库
iphone - 在 init 中声明 block 安全吗?
ios - 我怎么能改变占位符文本的固定字符的颜色,因为我有点困惑执行这个
ios - Swift:无法长按拖动单元格到 UICollectionViewController 和 UITableView 中的空白部分
ios - 如何在 iOS Swift4 中滚动时增加/减小 UILabel 字体大小以及宽度和高度
ios - NSURLSession 后台任务 : avoid duplicates
ios - 通过在 Swift 中共享 UIActivityViewController 在电子邮件上添加主题