【问题标题】:Table view inside of a collection view集合视图内的表视图
【发布时间】:2018-06-04 11:16:41
【问题描述】:

我有一个集合视图,每个集合视图单元格都有一个表格视图,每个集合视图单元格都有不同的数据。集合视图是使用情节提要完成的并且工作正常,但我似乎无法让 UITableView 在其中工作。集合视图代码如下,任何帮助表示赞赏。理想情况下,一种允许我使用故事板自定义表格视图单元格的技术,我使用的是 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))")
    }
}

我想在其中放置一个表格视图(列表),如下所示:

【问题讨论】:

标签: ios swift uitableview uicollectionview


【解决方案1】:

正在使用的UICollectionViewCell 类应该符合UITableViewDelegateUITableViewDataSource 协议,这些协议将初始化表格视图中的单元格

【讨论】:

    【解决方案2】:

    您的 UICollectionViewCell 类应确认协议、UITableViewDelegateUITableViewDataSource

    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()
        }
    }
    

    最后别忘了在你的 storyboard 中分配 TableView DataSource 和 Delegate

    【讨论】:

    • 我制作了表格视图数据源并委托给集合视图,它应该在集合视图单元格还是整个视图控制器上?
    • 委托应该在collectionView Cell上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多