点击一行将选择多行
我的桌面视图中有一个奇怪的问题。我想选择行并且只想显示复选标记。我有一个包含多个部分的表格视图。选择该行并显示复选标记没问题,但是在表格视图中向下 10 行,该行也会被选中???当选择多于一行时,再往下 10 行,会出现多个选择。
我正在使用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
}
}
I have a weird issue in my tableview. I want to select rows and only want to show the checkmark. I have a tableview with multiple sections. Selecting the row and displaying the checkmark is fine, but 10 rows down the tableview, that row will also get selected??? When selecting more than one row, again, 10 lines down, multiple selections appear.
I am using:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是选择另一行,而是重复使用同一复选标记的单元格。在
tableView:didSelectRowAtIndexPath:
中,您使用单元格来存储状态。在tableView:cellForRowAtIndexPath:
中,您没有解决该行是否应该有复选标记的问题。您可能想要做的是拥有一个
ivar
NSMutableSet
并在didSelectRow..
中添加或删除indexPath
从集合中,取决于其检查状态。然后在cellForRow..
中根据indexPath
在集合中的成员身份设置accessoryType
属性。编辑抱歉,我没有注意到您在数据模型中保存状态的注释。在这种情况下,您所要做的就是在
cellForRow...
中添加检查。由于这是一个常见问题,我将保留原始答案。That's not another row being selected, that's the same check-marked cell being reused. In
tableView:didSelectRowAtIndexPath:
you are using the cell to store state. IntableView:cellForRowAtIndexPath:
you are not addressing whether the row should have a check mark or not.What you will likely want to do is have an
ivar
NSMutableSet
and indidSelectRow..
either add or remove theindexPath
from the set, depending on it's checked status. Then incellForRow..
set theaccessoryType
property based on theindexPath
's membership in the set.Edit Sorry I didn't catch the comments where you save the state to in a data model. In that case all you have to do is add the check in
cellForRow...
. Since this is a common question I'll leave the original answer up.