Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Collection View in Custom Table View Cell

I have a table view with a custom Cell which gets it contents from an array, this all builds fine. I want to add to this content a Collection View of thumb nail images of varying number to each of the custom Table View Cells.

I have added the Collection to the Custom Cell in the Story Board. I have linked the Collection to the Custom Cell.m (not sure it should go there). I have created a Collection Custom Cell and linked the image in.

It is from here I am not sure, should the methods for building the collection view be placed in the the Custom Table Cell.m OR in the View Controller.m? I have an image from the Story Board, you can see the custom table cell and then (not so clear) at the bottom is a Collection View (horizontal scrolling) which I want to populate with images - just not sure how? I am also unsure what info may help so i am sorry if there is info missing.

Picture of Custom Table Cell

like image 763
Recycled Steel Avatar asked May 20 '13 10:05

Recycled Steel


2 Answers

You should put the Delagate and DataSource of the UICollectionView inside the custom UITableViewCell class.

Here is a nice tutorial.

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

Good Luck!

like image 53
Nikola Kirev Avatar answered Sep 22 '22 06:09

Nikola Kirev


Here is the solution for swift. you need to setup a collectionview data source and delegate into the tableview cell using tableView delegation method cellForRowAtIndexPath, after this use a collectionview datasource and delegate method into a viewController where you have confirmed tableveiew data source and 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 cell.setcollectionViewDataSourceDelegate(self, forRow:indexPath.rom) code sets a collectionview delegate and datasource into table view cell after this drag the collectionview outlet to tableview cell. Into the tableView cell add a mehod setcollectionViewDataSourceDelegate to set collectionView delegate as follow :

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 this use the collection view delegate method into you view 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 clear explanation visit this https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

like image 42
bikram sapkota Avatar answered Sep 24 '22 06:09

bikram sapkota