UITableView - Multiple Choice and One Choice

I have 2 sections in a UITableView.
I want the first section to allow multiple cells to be selected, and the second section to allow only one selection.
I tried the code but did not work very well.
Code, if possible, quickly. Thanks.

enter image description here

+6
source share
3 answers

Perhaps you could implement the table view delegate methods:

tableView(_:shouldHighlightRowAtIndexPath:)

and

tableView(_:didSelectRowAtIndexPath:)

... and determine (from indexPath.row and indexPath.section ) if the corresponding section supports single / multiple selection (this will depend on the user logic of the -eg data model: "Section 0 supports multiple selection, but section 1 is not") , and if it supports only one selection, check if there is already a selected row (by accessing tableView.indexPathsForSelectedRows ).

If there is already a selected row, you can:

  • Return false from tableView(_:shouldHighlightRowAtIndexPath:) and
  • Don't do anything (just return ) from tableView(_:didSelectRowAtIndexPath:) (I'm not sure if this method is actually called when you return false from shouldHighlight... , so maybe check it out).
+4
source

You can just try this. This solution works fine for me. Try, perhaps for others ...

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 0 { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .Checkmark } } else { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .Checkmark } } } func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 1 { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .None } } } 

Edited: For 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 } } } 
+2
source

If you want the selected row in section 2 to be the new selected row, this might work for you. Stay, 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 this will give you an idea.

0
source

All Articles