Search code examples
iosswiftsegue

How can I transfer multiple rows of a tableView to another ViewController


I'm trying to add a feature in my app, to add multiple members to one action at one time. The members are listed in a tableView and the user can select multiple rows at one time with the .allowsMultipleSelection = true function. I got the following code but this doesn't work. I think my idea would work but not in the way I have it in the code :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

       guard let destination = segue.destination as? AddMultipleMemberTransactionViewController,
        let selectedRows = multipleMemberTableView.indexPathsForSelectedRows else  {
            return
        }
        destination.members = members[selectedRows]
    }

Does somebody out here know, how I can solve this problem, because there is an error :

Cannot subscript a value of type '[Member?]' with an index of type '[IndexPath]'

I have the same feature in the app but just for one member. There I in the let selectedRows line after the indexPathForSelectedRow a .row. Is there a similar function for indexPathsForSelectedRows ?
Or is this the wrong way to do it?


Solution

  • You need

    destination.members = selectedRows.map{ members[$0.row] }