在汇集视图里面的表视图

问题描述 投票:0回答:2

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

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

Storyboard layout

ios swift uitableview uicollectionview
2个回答
1
投票

你的UICollectionViewCell类应该向Protocols,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()
    }
}

最后,不要忘记在故事板中分配TableView DataSource和Delegate


3
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.