UITableView - 多选和单选 [英] UITableView - Multiple selection AND single selection
问题描述
我的 UITableView 中有 2 个部分.
我希望第一部分允许多个单元格选择,第二部分只允许单选.
我尝试了一些代码,但效果不佳.
如果可能,请快速编码.谢谢.
I have 2 sections in my UITableView.
I want the first section to allow multiple cell selection and the second section to allow only single selection.
I tried some code but didn't work very well.
Code in swift if possible. Thank you.
推荐答案
也许你可以实现表视图的委托方法:
Perhaps you could implement the table view's delegate methods:
tableView(_:shouldHighlightRowAtIndexPath:)
和
tableView(_:didSelectRowAtIndexPath:)
...并确定(来自 indexPath.row
和 indexPath.section
)相关部分是否支持单选/多选(这将取决于您的数据模型的自定义逻辑 -eg: "Section 0
支持多选但 section 1
不支持"),如果只支持单选,检查是否已经有一行被选中(通过访问 tableView.indexPathsForSelectedRows
).
...and determine (from indexPath.row
and indexPath.section
) if the relevant section supports single/multiple selection (this will depend on your data model's custom logic -e.g.: "Section 0
supports multiple selection but section 1
does not"), and if it only supports single selection, check whether there is already a row selected (by accessing tableView.indexPathsForSelectedRows
).
如果已经有选定的行,您可以:
If there is a selected row already, you can:
- 从
tableView(_:shouldHighlightRowAtIndexPath:)
和 返回 - 从
tableView(_:didSelectRowAtIndexPath:)
什么都不做(只是return
)(我不确定当你返回false 时是否真的调用了这个方法
来自shouldHighlight...
,所以也许检查一下).
false
- Return
false
fromtableView(_:shouldHighlightRowAtIndexPath:)
, and - Do nothing (just
return
) fromtableView(_:didSelectRowAtIndexPath:)
(I'm not sure if this method is actually called when you returnfalse
fromshouldHighlight...
, so perhaps check it).
这篇关于UITableView - 多选和单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!