UITableView - 多选和单选 [英] UITableView - Multiple selection AND single selection

查看:54
本文介绍了UITableView - 多选和单选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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.rowindexPath.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:

  1. tableView(_:shouldHighlightRowAtIndexPath:)
  2. 返回 false
  3. tableView(_:didSelectRowAtIndexPath:) 什么都不做(只是 return)(我不确定当你返回 false 时是否真的调用了这个方法 来自 shouldHighlight...,所以也许检查一下).
  1. Return false from tableView(_:shouldHighlightRowAtIndexPath:), and
  2. Do nothing (just return) from tableView(_:didSelectRowAtIndexPath:) (I'm not sure if this method is actually called when you return false from shouldHighlight..., so perhaps check it).

这篇关于UITableView - 多选和单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆