7

我有一个带有自定义单元格的表格视图,它从数组中获取内容,这一切都很好。我想向每个自定义表格视图单元格添加不同数量的缩略图的集合视图。

我已将集合添加到故事板中的自定义单元格中。我已将 Collection 链接到 Custom Cell.m(不确定它应该去那里)。我创建了一个集合自定义单元格并将图像链接到其中。

从这里我不确定,构建集合视图的方法应该放在自定义表格 Cell.m 中还是放在 View Controller.m 中?我有一个来自故事板的图像,您可以看到自定义表格单元格,然后(不太清楚)底部是一个收藏视图(水平滚动),我想用图像填充它 - 只是不确定如何?我也不确定哪些信息可能会有所帮助,所以如果缺少信息,我很抱歉。

自定义表格单元格的图片

4

2 回答 2

4

您应该将DelagateandDataSource放在UICollectionView自定义UITableViewCell类中。

这是一个很好的教程

它是关于 tableview 单元格内的 tableview,但想法非常相似。

祝你好运!

于 2013-06-15T05:58:00.257 回答
2

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/

于 2016-08-05T17:39:35.550 回答