UITableView - Multiple selection AND single selection

前端 未结 4 1535
半阙折子戏
半阙折子戏 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:38

    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.

提交回复
热议问题