简体   繁体   English

表格视图单元格内的集合视图单元格的索引路径

[英]Index path of a collection view cell that is inside of a table view cell

I have a UICollectionView nested inside of a table view cell. 我有一个嵌套在表视图单元格内的UICollectionView。 How can I get the index path of the horizontal row of the collectionview cell? 如何获取collectionview单元格水平行的索引路径? I tried to use 我尝试使用

        let index = cell.tag

        print(index as Any) 

to print which index was being selected not knowing that it will print a value of zero no matter what cell I select. 要打印选择的索引,无论我选择了哪个单元格,都不知道它将打印零值。 I apologize that I am not experienced with collection views. 抱歉,我对集合视图没有经验。 Any help is much appreciated. 任何帮助深表感谢。 Thanks. 谢谢。

You should design a protocol handling nested UIElement interactions. 您应该设计一个协议来处理嵌套的UIElement交互。 It makes your mind clearer and allocate better addresses 它使您头脑更清晰,分配更好的地址

class ViewwithTable:UIViewController,UITableViewDataSource,collectionCell_delegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "temp", for: indexPath) as! NestedViewwithCollection
        cell.delegate = self
        return cell
    }

    func didPressed(indexPath: IndexPath) {
        //the pressed index!
    }
}

class NestedViewwithCollection:UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate{
    var delegate:collectionCell_delegate?

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

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

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let del = delegate{
            del.didPressed(indexPath: indexPath)
        }
    }

}

protocol collectionCell_delegate {
    func didPressed(indexPath:IndexPath)
}

Use this code for tableView 将此代码用于tableView

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return model.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    return cell
}

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)
}

Collection View 集合视图

extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource 
{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return model[collectionView.tag].count
}

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

    cell.backgroundColor = model[collectionView.tag][indexPath.item]

    return cell
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速在单独的方法中获取集合视图单元格的索引路径 - Getting the index path of collection view cell inside separate method in swift 表视图单元格内的集合视图 - Collection View inside Table View Cell 如何在swift中同时显示表格视图单元格索引路径和集合视图索引路径? - How to show table view cell index path and collection View Index path at the same time in swift? 是否可以在 swift 中显示表格视图单元格索引路径和集合视图索引路径? - Is it possible to show table view cell index path and collection View Index path in swift? 在tvos应用程序的表视图单元格中单击集合视图单元格 - Click on collection view cell inside a table view cell in tvos application 将数据从集合视图单元内的表视图单元传输到另一个集合视图单元(带有图片!) - Transfer data from table view cell inside a collection view cell, to another collection view cell [with picture!] 无法访问表格视图单元的索引路径 - Index Path for the table view cell is not being accessible 无法在表格视图单元格内滚动集合视图(在情节提要中创建) - Unable to scroll Collection view inside Table view cell (created in Storyboard) 如何在表格视图单元格内创建集合视图 - How to create collection view inside table view cell iOS Swift:自动调整大小的集合视图嵌套在表格视图单元格中 - iOS swift: autoresize collection view nested inside a table view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM