Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Multiple Selection

How can I add a UITableView into my View-Based Application where the user will tap on more than one cell, and it will become selected, exactly like the Clock app's "New Alarm" setting named "Repeat" (Clock>Alarms> + >Repeat), and how can I get all of the selected cells in an array?

like image 648
Hank Brekke Avatar asked Jun 14 '10 21:06

Hank Brekke


People also ask

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What is a UITableView?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.


2 Answers

For multiple selection, add the line below in viewDidLoad()

tableView.allowsMultipleSelection = true 

Configure each cell after dequeuing (or initializing) it in tableView(_:cellForRowAt:)

let selectedIndexPaths = tableView.indexPathsForSelectedRows let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath) cell.accessoryType = rowIsSelected ? .checkmark : .none // cell.accessoryView.hidden = !rowIsSelected // if using a custom image 

Update each cell when it's selected/deselected

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {     let cell = tableView.cellForRow(at: indexPath)!     cell.accessoryType = .checkmark     // cell.accessoryView.hidden = false // if using a custom image }  override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {     let cell = tableView.cellForRow(at: indexPath)!     cell.accessoryType = .none     // cell.accessoryView.hidden = true  // if using a custom image } 

When you're done, get an array of all the selected rows

let selectedRows = tableView.indexPathsForSelectedRows 

and get the selected data, where dataArray maps to the rows of a table view with only 1 section

let selectedData = selectedRows?.map { dataArray[$0.row].ID } 
like image 153
Raphael Oliveira Avatar answered Oct 08 '22 16:10

Raphael Oliveira


In your implementation of -tableView:didSelectRowAtIndexPath: you would set the table view cell's accessoryType property depending on its current value (so it would toggle on and off with multiple taps). For example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {     UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];      if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {         cell.accessoryType = UITableViewCellAccessoryNone;     } else {         cell.accessoryType = UITableViewCellAccessoryCheckmark;     } } 

You could either maintain an array of selected states in addition to the cells' own accessory type state, or iterate over the cells in the table view querying for each one's state in order to read out the selected rows.

like image 26
Brendan Berg Avatar answered Oct 08 '22 16:10

Brendan Berg