【问题标题】:how to pass data data from collection view cell to view controller如何将数据数据从集合视图单元传递到视图控制器
【发布时间】:2018-11-09 11:46:50
【问题描述】:

我在 collectionview 单元格中有一个按钮,我正在尝试从视图控制器访问当前标题。

但是当我尝试访问时,我得到的标题是默认标题“按钮”

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
             let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "graphCell", for: indexPath) as! graphCell


             let row = indexPath.row

            if row == 0{
                print("Compare")
            }

            else if row == 1{
                let optSelected=cell.chooseBTN.title(for: .normal)
                print(option)
                if optSelected=="Sales"{
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)

                    let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC

                    vc.buttonClicked = "Sales"; navigationController?.pushViewController(vc, animated: true)
                }
                else if optSelected=="Collection"{
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)

                    let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC

                    vc.buttonClicked = "Collection"; navigationController?.pushViewController(vc, animated: true)

                }

            }

            else if row == 2{
                print("PRO-wise")
            }
            else if row == 3{
                print("TAT")
            }

        }

【问题讨论】:

  • 不要在不是collectionView(_:cellForRowAt:)的其他地方打电话给dequeueReusableCell()。您可能想使用 let cell = collectionView.cell(forRowAt:indexPath) (或类似的东西)。但您也可以,最好从帮助您构建单元格的数据源/数组中检索值。
  • 您不应使用 dequeueReusableCell() 或 cellForRow 来获取标题。我建议您使用行索引从数组/列表或您拥有的任何数据源中检索标题。或者,如果它有一个复杂的逻辑来检索标题,则在呈现单元格时使用另一个数组来保存标题,然后使用 rowIndex 和该数组来检索标题

标签: ios swift uicollectionviewcell swift4.2


【解决方案1】:

替换

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

与

 let cell = collectionView.cellForItem(at:indexPath) as! graphCell

还有这个

let optSelected=cell.chooseBTN.title(for: .normal)
print(option)
if optSelected=="Sales"{
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC

    vc.buttonClicked = "Sales"; navigationController?.pushViewController(vc, animated: true)
}
else if optSelected=="Collection"{
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC

    vc.buttonClicked = "Collection"; navigationController?.pushViewController(vc, animated: true)
}

可以替换为

  let vc =  storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC
  vc.buttonClicked = cell.chooseBTN.title(for: .normal)
  navigationController?.pushViewController(vc, animated: true)

【讨论】:

    猜你喜欢
    • 2017-10-25
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多