Rohan Vasishth Rohan Vasishth - 2 months ago 6
Swift Question

Is there any way to grab the text and detail text labels of multiple selected table view cells in swift?

I have some code right now that gets what cells were selected. The following is my code for doing that:

if let selectedUserRows = self.tableView.indexPathsForSelectedRows {

self.groupUserArray.append(selectedUserRows)
for index in selectedUserRows {
let text = groupUserArray[index.row]
print(text)
}
}


The new error is saying that the index is out of range!
I was trying to use the logic for grabbing one selected cells text but it does not work. So I was wondering if anyone knew how to grab multiple selected cell's text?

Any help would be appreciated!

Answer

self.tableView.indexPathsForSelectedRows returns a [IndexPath]? so if you obtain an array of all the rows and sections of you selected rows. I suppose you are using some sort of collection to populate your tableView (in my example I will call it textArray: [String] ) so you can do something like that to get all the text you need:

if let selectedUserIndexes = self.tableView.indexPathsForSelectedRows { for index in selectedUserIndexes { let text = textArray[index.row] } }