If i click on a particular cell of collection view then the data should be shown related to that cell of collection view in the table view in the same view controller (not in the other view controller)
If i click on a particular cell of collection view then the data should be shown related to that cell of collection view in the table view in the same view controller (not in the other view controller)
2
Answers
On
didSelectItemAt
method of collectionView you reset the data source of tableView and reload tableView that should do the job.Regardless of having one or multiple view controllers. A good practice is to have a data structure that fits your visual state. For your case I would expect to have something like
But to be more concrete let’s assume that we have a collection view of users where after pressing a certain user a table view should update a list of friends for that user.
A data source like the following could show that:
Now to create a structure that is more fit for your display you could have something like this:
In your view controller you would create a new instance of your data model. Probably in
viewDidLoad
but this all depends on how you collect your data.For instance
Now your data source implementations can use
dataModel?.allUsers
for collection view anddataModel?.selectedUser?.friends
for your table view.Now all that is left is interaction. When a collection view cell is pressed you would do
note here that a new user is being selected using
selectedUser = user
and then a reload is triggered for table view callingtableView?.reloadData()
which will force the table view to call data source methods and get the new information.An even better approach may be to listen for changes in selected user by creating your own delegates on your data model and respond to that. But that is already out of scope for this question.
I hope this puts you on the right path.