UITableView - Multiple selection AND single selection

前端 未结 4 1538
半阙折子戏
半阙折子戏 2020-12-31 14:15

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 co

4条回答
  •  清歌不尽
    2020-12-31 14:33

    Perhaps you could implement the table view's delegate methods:

    tableView(_:shouldHighlightRowAtIndexPath:)

    and

    tableView(_:didSelectRowAtIndexPath:)

    ...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. 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).

提交回复
热议问题