特定于UICollectionViewCell的TableView以编程方式? [英] TableView inside specific UICollectionViewCell programmatically?

查看:101
本文介绍了特定于UICollectionViewCell的TableView以编程方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在UICollectionViewCell内添加一个TableView,所以我想自己从TableView中管理该单元格. 因此更清楚一点,如果indexPath.row为2,那么我想在集合视图indexPath.row中调用tableView单元格.

i am trying to add a TableView inside UICollectionViewCell, so i want to manage that cell by myself from TableView. So to be more clear, if indexPath.row is 2, then i want to call my tableView cell's inside collectionview indexPath.row.

请检查图片,我已经用红色制成了我想要的颜色. 我使用UICollectionViewController和UICollectionViewControllerFlowLayout以编程方式创建了所有内容.

Please check the picture, i have made with red colour what i want to do. I created everything programmatically, using UICollectionViewController and UICollectionViewControllerFlowLayout.

推荐答案

对于那些需要它的人,我找到了解决方法:

For those who need it, i found the solution:

class CustomizedCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {

    var tableView = UITableView()
    let cellIdentifier: String = "tableCell"

    override func layoutSubviews() {
        super.layoutSubviews()

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdentifier)

        cell.textLabel?.text = "1 CUP"
        cell.detailTextLabel?.text = "Whole"

        return cell
    }
}

然后在CollectionView的viewDidLoad方法中执行此操作:

Then at viewDidLoad method at CollectionView i did this:

collectionView?.register(CustomizedCell.self, forCellWithReuseIdentifier: "cell")

然后我在UICollectionView的cellForRowAt indexPath方法中这样调用:

And after that i have called like this at cellForRowAt indexPath method of UICollectionView:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomizedCell

    return cell
}

这篇关于特定于UICollectionViewCell的TableView以编程方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆