【问题标题】:Swift: How to nest a UITableView with varying table row counts inside a UICollectionView cell?Swift:如何在 UICollectionView 单元格内嵌套具有不同表行数的 UITableView?
【发布时间】:2016-09-19 05:26:21
【问题描述】:

viewcontroller 符合四种协议:

class transferResultsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate {
}

UICollectionView 包含 4 个单元格:

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

还有一个简单的实现:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCellForTransferResults

    // Labels of cells are set up here --> irrelevant for question

    return cell
}

有一个 TableView 嵌套在 CollectionView 单元格中,有 1 个部分:

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

问题:nr。所有四个 collectionView 单元格中的 tableview 行数都可以通过数组访问:

let nrOfRowsArray = [2, 3, 2, 1] // e.g. the 1st collectionnviewcell has a tableview embedded that contains 2 row

如何为每个 collectionviewcell 返回 tableview 内的 numberOfRows?

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var numberOfRows = ? // How to store the correct value here?
    return numberOfRows
}

让它返回正确的 cellForRowAtIndexPath?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "identifier"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! nestedTableViewCell

    // Labels of cells are set up here --> irrelevant for question

    return cell
}

【问题讨论】:

    标签: swift uitableview uicollectionview


    【解决方案1】:

    在您的tableView数据源和委托方法中,您需要一些方法来确定tableView在哪个collectionViewCell。有几种方法,但最简单的可能是使用tableView的tag属性来指示collectionViewCell的item

    大概你的CollectionViewCellForTransferResults 有一个tableView 属性。当您将 collectionViewCell 出列时,请像这样设置tag

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCellForTransferResults
        cell.tableView.tag = indexPath.item
    
        // Labels of cells are set up here --> irrelevant for question
    
        return cell
    }
    

    现在在您的tableView 数据源和委托方法中,您可以测试tableView 参数的tag 属性以确定collectionViewCell。例如:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let tvTag = tableView.tag 
        let numberOfRows = nrOfRowsArray[tvTag]
        return numberOfRows
    }
    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "identifier"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! nestedTableViewCell
    
        // Labels of cells are set up here --> irrelevant for question
        let tvTag = tableView.tag 
        // configure the cell for the row given by indexPath.row in
        // the collectionViewCell given by tvTag
    
        return cell
    }
    

    这应该可以正常工作

    1. 您的集合视图中只有一个部分(否则可能有多个集合视图单元格具有相同的item,尽管位于不同的sections);和
    2. 您不要添加/删除集合视图单元格(因为其他 tableViews 上的 tag 最终可能与集合视图 item 值不同步)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 2018-08-02
      • 2017-06-12
      • 1970-01-01
      相关资源
      最近更新 更多