I have created a table view and add a custom table view cell . Inside the custom table view cell, i have added 4 labels and passed an array on each on of them . i have also enabled multiple row selection . i want to pass the selected rows to the next screen and display it there .
-
What part of the selected cells do you want to pass to the next screen? The indexPath, or the labels, or something else? How do you go to the next screen with multiple selected cells? – koen Nov 30 '19 at 19:35
-
Add the `selected` information to your data model. Then pass all items with `selected == true` to the next controller. – vadian Nov 30 '19 at 20:10
-
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { for (index, element) in amountSelected.enumerated() { if element == invoice[indexPath.row] { amountSelected.remove(at: index) } } print(amountSelected) } – Ahmed mohamed Dec 01 '19 at 14:27
-
Declare amountSelected first in controller and on button click code, you can see in below answer @IBAction func doneClick(_ sender: Any) { }. i declare tableViewSelectedRow in next screen. Hope it's help you. – Anjali Shah Dec 01 '19 at 17:14
2 Answers
When you select row, every time UITableView
didSelect call so you can make array for store value of selectedRow, If already in array than remove row.
selectedRow array you can pass in next screen use it.
For delete button i use mk_addTapHandler Button Extention you can see here
In my project same issue, but as per my design i put one button on cell for delete, i solved so You can use below code :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellDemo", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
cell.btnDelete.mk_addTapHandler { (btn)
for (i,objValue) in selectedRow.enumerated() {
if indexPath.row == objValue {
self.selectedRow.remove(at: i)
for (i,objValue) in selectedRow.enumerated() {
if indexPath.row < objValue {
selectedRow[i] = objValue - 1
}else {
selectedRow[i] = objValue + 1
}
}
}
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedRow.append(indexPath.row)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
for (index, element) in selectedRow.enumerated() {
if indexPath.row == element {
selectedRow.remove(at: index)
}
}
}
@IBAction func doneClick(_ sender: Any) {
if let VC = self.storyboard?.instantiateViewController(withIdentifier: "nextScreen") as! nextScreen
VC.tableViewSelectedRow = selectedRow
self.navigationController?.pushViewController(VC, animated: true)
}
}

- 720
- 7
- 21
-
Be aware that this solution will cause problems if cells can be inserted or removed. – vadian Nov 30 '19 at 19:36
-
yes @vadian but when you delete please remove row from array and when you insert cell as first than increase +1 for all array value – Anjali Shah Nov 30 '19 at 19:40
-
2This is not sufficient. If you remove the cell at index 2, all index paths > 2 change which invalidates your index array. – vadian Nov 30 '19 at 20:02
-
I solved problem in my code and edit answer, now you can try - Thank you – Anjali Shah Nov 30 '19 at 21:37
-
If you have already enabled multi-row selection in the tableView it's relatively straightforward. In your button action method get the indexPaths of your selected rows and then use them to extract the data from your datasource and inject them into the next viewController. The below gives you core functionality but as you have not shared your code for datasource or table design, or indicated whether you are using IB or code for your app, you will have to make it fit your environment.
Assumptions:
- you have a simple tableView with only one section and all rows are possible data for the next VC
- you are doing your UI in code, and want to push rather than present the next VC
- your data for your view is in an array called
mainData
- your next view controller is a class called
NextViewController
@objc doneButtonTapped(_ sender: UIButton) {
let indexPaths = tableView.indexPathsForSelectedRows
let selectedData = indexPaths.map{mainData[indexPath.row]}
let nextVC = NextViewController()
nextVC.data = selectedData
navigationController?.pushViewController(nextVC, animated: true)
}

- 3,840
- 1
- 12
- 20