简体   繁体   中英

how to send data from uitableviewcontroller to uitableviewcell

I have the following situation

A TableViewController (ExploreViewController) has custom cells - CategoryTableViewCell. Each cell has a collection view with UICollectionViewCells.

I would like the CategoryTableViewCell to contact the ExploreViewController when displaying the collection view cells to determine what data it needs to display. The data is dependent on the index path row of the table view cell and on the index of the collection view cell.

How can I accomplish this using delegates?

I wouldn't do it that way. I would pass the data each cell needs to it as a model object in the table view controller's cellForRow(at:) method. If a cell's data changes, pass it a new model object. Then the cell can use that model data to respond to data source calls from its child collection view.

If you are determined to use the delegate pattern, or something like it:

Let's call it a data source instead of a delegate. Design a CategoryTableViewCellDataSource protocol. When you create a CategoryTableViewCell, set the table view controller as its data source.

Have the cell ask it's data source for the data to display. The delegate protocol would use the cell's position to figure out it's index path, and then fetch the data from the model and return it. (You'd find the midpoint of the cell's content view, convert the point to the table view's coordinate system, and then use the table view's indexPathForRow(at:) method to figure out the index path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM