Reputation: 659
I use the following function so I can select 2 rows in a table:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedCellTitle = self.communityPlayers[indexPath.row]
cellId = indexPath.row
//print (self.communityPlayerIds[indexPath.row])
if let cell = tableView.cellForRow(at: indexPath) {
if cell.isSelected {
cell.accessoryType = .checkmark
}
}
if let sr = tableView.indexPathsForSelectedRows {
print("didSelectRowAtIndexPath selected rows:\(sr)")
if sr.count == 2{
print ("one ", sr[0])
print ("two ", sr[1])
}
}
}
the print("didSelectRowAtIndexPath selected rows:\(sr)")
line outputs something like this:
didDeselectRowAtIndexPath selected rows:[[0, 2], [0, 3]]
I want to access the second int only in each array (discarding the 0's). I have tried using print ("one ", sr[0])
to attempt this but this outputs the following:
one [0, 2]
In the example above I only want to access the 2
so I can store that in a separate variable.
How can I access the second part only of these arrays? (the 2, and 3)
Upvotes: 1
Views: 70
Reputation: 2594
If you want an array containing just the second numbers, you can use a map
, like this:
sr.map { $0[1] }
Or, since they are index paths, this is neater:
sr.map { $0.row }
Upvotes: 3
Reputation: 672
Just to be clear, sr = [[0,2],[0,3]]
?
If so, you have two arrays.
let firstArray = sr[0]
//you will see this would print as [0,2]
let numberTwo = firstArray[1]
//you will see this would print as 2
Upvotes: 2