如何在UitableviewCell

时间:2016-05-21 01:14:47

标签: ios uitableview uicollectionview uicollectionviewcell

我在uitableviewcell中嵌入了一个uicollectionview单元格。 uicollectionview的数据源和委托连接到TableviewController。我似乎无法计算如何填充每个tableview单元格中的集合视图。这是我的TableviewController中的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];




    // Configure the cell...

    return cell;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];



    // Configure the cell

    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return 13;
}

3 个答案:

答案 0 :(得分:1)

表格视图单元格需要是它们所拥有的集合视图的数据源。只需将必要的数据传递到cellForRowAtIndexPath(_:)方法中的表格视图单元格,并让单元格填充各自的集合视图。

答案 1 :(得分:1)

这就是我之前所做的。

collectionView是我拖到自定义表格视图单元格xib的界面构建器上的IBOutlet。

在自定义表格视图单元格类

    override func layoutSubviews() {
        super.layoutSubviews()
        //configure collection view
        collectionView.registerNib(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView.scrollEnabled = false
        collectionView.pagingEnabled = true
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = UIColor.whiteColor()
        //....
    }

然后实现您的委托和数据源方法

PS:对不起,它是用Swift编写的,但你可以轻松转换为Obj-C。

答案 2 :(得分:0)

我建议在视图控制器而不是表视图单元中实现集合视图的数据源和委托方法。例如,假设这是您的表格视图单元格的类:

class TableViewCell: UITableViewCell {

    @IBOutlet private weak var collectionView: UICollectionView!

}

您可以在表格视图单元格的类中创建一个方法来设置集合视图的数据源和委托方法。像这样:

class TableViewCell: UITableViewCell {

    @IBOutlet private weak var collectionView: UICollectionView!

    func setCollectionViewDataSourceDelegate <D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>
    (dataSourceDelegate: D, forRow row: Int) {

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = row
        collectionView.reloadData()
    }
}

然后,在您的UIViewController类中,您可以执行以下操作:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    guard let tableViewCell = cell as? TableViewCell else { return }

    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}

,您的UIViewController可以遵循UICollectionViewDataSourceUICollectionViewDelegate协议:

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10 // 10 here is just an example.
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", 
        forIndexPath: indexPath)

        return cell
    }
}

Ash Furrow在a nice blog post中写了关于UITableView单元内的UICollectionViews的信息,也许对您有帮助。

相关问题