集合视图内的表视图 [英] Table view inside of a collection view
问题描述
我有一个集合视图,每个集合视图单元格都有一个表视图,每个集合视图单元格都有不同的数据.集合视图是使用故事板完成的并且工作正常,但我似乎无法让 UITableView 在其中工作.集合视图代码如下,任何帮助表示赞赏.理想情况下是一种允许我使用故事板自定义 tableview 单元格的技术,我使用的是 Swift 4.
I have a collection view, and each collection view cell will have a table view which will have different data for each collection view cell. The collection view is done using a storyboard and works fine, but I can not seem to get a UITableView to work inside of it. The Collection view code is below and any help is appreciated. Ideally a technique that allows me to use storyboard to customise the tableview cell, and I am using Swift 4.
extension StoryboardViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt
indexPath: IndexPath) {
print("Selected Cell #\(indexPath.row)")
}
}
extension StoryboardViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: "CollectionViewCell"), for: indexPath) as! StoryboardCollectionViewCell
cell.label.text = "Cell #\(indexPath.row)"
return cell
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print("Current centered index: \(String(describing: centeredCollectionViewFlowLayout.currentCenteredPage ?? nil))")
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
print("Current centered index: \(String(describing: centeredCollectionViewFlowLayout.currentCenteredPage ?? nil))")
}
}
我想在其中放置一个表格视图(列表),如下所示:
And I want to put a table view (list) inside of it like so:
推荐答案
你的 UICollectionViewCell
类应该向协议确认,UITableViewDelegate
和 UITableViewDataSource
Your UICollectionViewCell
Class should confirm to Protocols, UITableViewDelegate
and UITableViewDataSource
import UIKit
class CollectionViewCell: UICollectionViewCell,UITableViewDelegate,UITableViewDataSource {
//MARK: TableViewDelegateAndDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Configure your cell here
return UITableViewCell()
}
}
最后不要忘记在故事板中分配 TableView DataSource 和 Delegate
Finally don't forget to assign TableView DataSource and Delegate in your storyboard
这篇关于集合视图内的表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!