Insert collection into user table

I have a table view with a custom cell that gets this content from an array, all of this is fine. I want to add a collection to this content. View images of nails with a variable number for each of the custom table cells.

I added the collection to the custom cell in History. I linked the collection to Custom Cell.m (not sure if it should go there). I created the Custom Cell assembly and linked the image.

It is from here that I am not sure whether the methods for constructing the collection view should be placed in the user table Cell.m OR in the view manager .m? I have an image from the Story Board, you can see a custom table cell, and then (not so clear) at the bottom - this is the collection view (horizontal scrolling) that I want to fill with images - just don't know how to do it? I also do not know what information can help, so I am sorry if there is no information.

Picture of Custom Table Cell

+7
source share
2 answers

You must put the Delagate and DataSource from the UICollectionView in the custom class UITableViewCell .

Here is a good tutorial .

It's about a tableview inside a tableview cell, but the idea is pretty similar.

Good luck

+4
source

Here is a quick solution. you need to configure the collection data source and delegate it to the tableview cell using the tableView delegation method cellForRowAtIndexPath, then use the data collection method and delegation method in viewController, where you validated the data source and the tableveiew delegate. Here is the code for mainViewController:

 extension MainViewController:UITableViewDataSource, UITableViewDelegate { // .....do some table view setup like numberOfRowsInSection ...... func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("yourReusecellIdentifier", forIndexPath: indexPath) as! yourCustomCell cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) return cell } } 

Here, the code cell.setcollectionViewDataSourceDelegate (self, forRow: indexPath.rom) sets the collection delegate and data source to the table view cell, then drag the collection output to the tableview cell. In the tableView cell, add mehod setcollectionViewDataSourceDelegate to set the delegate of the View collection as follows:

 class yourCustomCell: UITableViewCell { //MARK:- Properties @IBOutlet weak var collectionView: UICollectionView! //MARK:- initialization methods override func awakeFromNib() { super.awakeFromNib() setupView() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } //MARK:- Setup collectionView datasource and delegate func setCollectionViewDataSourceDelegate <D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>> (dataSourceDelegate: D, forRow row: Int) { collectionView.delegate = dataSourceDelegate collectionView.dataSource = dataSourceDelegate collectionView.tag = row collectionView.reloadData() } } 

After that, use the delegate method of viewing the collection as a controller:

 extension MainViewController: UICollectionViewDelegate, UICollectionViewDataSource { func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 2 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewReuseIdentifier", forIndexPath: indexPath) as! YourCollectionViewCustomCell .......cell configure.... return cell } } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("get selected collectionview itemindex \(indexPath.row)") } } 

For a clear explanation, visit https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

+2
source

All Articles