xcode 在自定义表格视图单元格中嵌入集合视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16647871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:49:44  来源:igfitidea点击:

Embed Collection View in Custom Table View Cell

xcodecocoatableviewcell

提问by Recycled Steel

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.

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

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.

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

Picture of Custom Table Cell

自定义表格单元格图片

采纳答案by Nikola Kirev

You should put the Delagateand DataSourceof the UICollectionViewinside the custom UITableViewCellclass.

你应该把DelagateDataSource该的UICollectionView内部自定义UITableViewCell类。

Here is a nice tutorial.

这是一个很好的教程

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

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

Good Luck!

祝你好运!

回答by bikram sapkota

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:

这是swift的解决方案。您需要设置一个collectionview数据源并使用tableView委托方法cellForRowAtIndexPath委托到tableview单元格中,然后使用collectionview数据源和委托方法到viewController中,您已经确认了tableview数据源和委托。这是 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 :

这里 cell.setcollectionViewDataSourceDelegate(self, forRow:indexPath.rom) 代码将 collectionview 委托和数据源设置到 table view cell 中,然后将 collectionview outlet 拖动到 tableview cell。在 tableView 单元格中添加一个方法 setcollectionViewDataSourceDelegate 来设置 collectionView 委托,如下所示:

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/

如需明确说明,请访问此https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/