UITableView - Multiple selection AND single selection

陌路散爱 提交于 2019-12-18 13:16:23

问题


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.


回答1:


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



回答2:


You can simply try this. This solution works for me perfectly. Give it a try maybe worked for others...

Swift-4

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }
    }
    else {
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if indexPath.section == 1 {
        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
        }
    }
}



回答3:


This is easily achievable in two lines as follows: (Swift 4)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if sectionAllowsMultipleSelection {
        if let indexPathsInSection = tableView.indexPathsForSelectedRows?.filter ({ $0.section == indexPath.section && $0.row != indexPath.row }) {
            for selectedPath in indexPathsInSection {
                tableView.deselectRow(at: selectedPath, animated: false)
            }
        }
    }
}



回答4:


If you want the selected row in section 2 to be the new selected row, this might work for you. Else, go with @NicolasMiari's answer.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 1 {
        for i in 0..tableView.numberOfRowsInSection(indexPath.section) - 1 {
            let cell: UITableViewCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: indexPath.section))!
            if (i == indexPath.row) {
                cell.accessoryType = .Checkmark
                cell.selected = false
            }
            else {
                cell.accessoryType = .None
            }
        }
    }
    else {
        //Do whatever for the first section
    }
}

Not very elegant, but hopefully it will give you an idea.



来源:https://stackoverflow.com/questions/32857091/uitableview-multiple-selection-and-single-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!