首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataInput用于UITableviewCell中的垂直UICollectionView

DataInput用于UITableviewCell中的垂直UICollectionView
EN

Stack Overflow用户
提问于 2018-02-27 15:40:19
回答 2查看 57关注 0票数 0

我的问题是,如何将UICollectionView实现为UITableViewCell (我的UITableViewController),并填充将从UITableViewController加载到UICollectionView中的数据。

我知道如何设置不同的组件,但我不知道如何通过我的UICollectionView-cellForRow...方法填充数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-27 16:01:40

你只要经历这一切-

创建一个通常的UITableView,在UITableViewCell中创建UICollectionView。您的collectionView委托和数据源应该符合该UITableViewCell。

在你的ViewController里

代码语言:javascript
复制
// Global Variable
var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

tableView = UITableView(frame: self.view.bounds)
    tableView.delegate = self
    tableView.dataSource = self
    self.view.addSubview(tableView)

    tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "NormalCell")
}

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 3 {
        var cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell", forIndexPath: indexPath) as! TableViewCell
        cell.backgroundColor = UIColor.groupTableViewBackgroundColor()
        return cell

    } else {
        var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("NormalCell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = "cell: \(indexPath.row)"

        return cell
    }
}

正如您所看到的,我已经创建了两个不同的单元格,一个自定义的TableViewCell只在行索引为3时返回,而在其他索引中则返回一个基本的UITableViewCell。

定制的"TableViewCell“将有我们的UICollectionView。因此,创建一个UITableViewCell子类并写下下面的代码。

代码语言:javascript
复制
    import UIKit

    class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

    var collectionView: UICollectionView!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal

        collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView.backgroundColor = UIColor.clearColor()

        self.addSubview(collectionView)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // MARK: UICollectionViewDataSource
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! UICollectionViewCell
    if indexPath.row%2 == 0 {
        cell.backgroundColor = UIColor.redColor()
    } else {
        cell.backgroundColor = UIColor.yellowColor()
    }

    return cell
}
}

有关更多信息,您可以看到这个存储库- https://github.com/DahanHu/DHCollectionTableView

希望这能解决你的问题。谢谢。

票数 1
EN

Stack Overflow用户

发布于 2018-02-27 15:51:52

如果我可以就如何处理这个问题提出建议,我将为存储在表视图控制器上的引用的单元格创建一个数据源对象。请考虑以下几点:

代码语言:javascript
复制
class TableCell: UITableViewCell {

    var collectionView: UICollectionView!
}

class TableCellDataSource: NSObject, UICollectionViewDataSource {

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

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
}

class TableViewController: UITableViewController {

    var cellDataSources: [TableCellDataSource] = []


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = TableCell()
        let dataSource = TableCellDataSource()
        cell.collectionView.dataSource = dataSource
        self.cellDataSources.append(dataSource)
    }

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

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

请注意,我现在如何控制每个单元格的集合视图从主表视图中显示的内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49012526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档