UITableView - Multiple selection AND single selection

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

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

提交回复
热议问题