Sergey_VC
Sergey_VC

Reputation: 103

Create a tableview, based on indexPathsForSelectedRows

I have two TableView ViewControllers - A and B. In "TableViewController A" each row of a table is a name, taken from a names array:

var names = ["Alex", "Helen", "Kate", "Margo", "Mike", "Tom"] 

In "TableViewController A" I can choose several items from a list. As a result I get an indexPathsForSelectedRows. Lets say, I have chosen Alex and Kate - so indexPathsForSelectedRows will be [[0,0], [0, 2]]. After this indexPathsForSelectedRows via segue is send to "TableViewController B".

The question is: What should be written in cellForRowAt method of "TableViewController B" so it shows only those items, that has been selected in "TableViewController A"?

I understand, that somehow have to teach a table in "TableViewController B" that its first cell should take 0 object from names array and second cell should take object #2. But up to now I'm failing to figure out a proper code for this.

Upvotes: 1

Views: 61

Answers (1)

veebha
veebha

Reputation: 47

  1. Make a dictionary of selected IndexPaths. Use didSelectRowAtIndexPath and didDeselectRowAtIndexPath to add and remove key/value pairs to the dictionary.
  2. Pass this dictionary to ViewController B
  3. Display names in table of ViewController B based on comparison of dictionary index

Hope it helps! Happy Coding....

Upvotes: 1

Related Questions