【发布时间】:2017-01-30 12:19:03
【问题描述】:
我有一个包含多个集合视图的视图控制器。
每个集合视图都使用与 xib 相同的自定义单元格。在这个 xib 中,我有一个按钮。
collectionViews 的名字是
1) manPerfumeCV
2) womanPerfumeCV
3) kidsPerfumeCV
在cellForItemAt 里面我有cell.productCart.tag = indexPath.row
let cell:iPhoneCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "iPhoneCell", for: indexPath) as! iPhoneCollectionViewCell
if collectionView == self.womanPerfumeCV {
let prods = womanPerfumeData[indexPath.row]
cell.configureCell(products: prods)
cell.productCart.tag = indexPath.row
} else if collectionView == self.manPerfumeCV {
let prods = manPerfumeData[indexPath.row]
cell.configureCell(products: prods)
cell.productCart.tag = indexPath.row
} else if collectionView == self.kidsPerfumeCV {
let prods = kidsPerfumeData[indexPath.row]
cell.configureCell(products: prods)
cell.productCart.tag = indexPath.row
}
在同一个视图控制器中,我对 xib 文件中的按钮执行此操作
@IBAction func iPhoneAddToCart(_ sender: AnyObject) {
let butt = sender as! UIButton
let indexP = IndexPath(row: butt.tag, section: 0)
let cell = manPerfumeCV.cellForItem(at: indexP) as! iPhoneCollectionViewCell
print(manPerfumeData[indexP.row].price)
}
每个集合视图都有自己的 [Products] 数组。
1) manPerfumeData
2) womanPerfumeData
3) kidPerfumeData.
在我的代码中,如果我用 manPerfumeData 点击位于第一个集合视图中的按钮,它会很好地打印价格。
虽然如果我按下第二个或第三个集合视图上的按钮,应用程序就会崩溃。
有什么方法可以从他按下按钮的收藏视图中知道,以便我可以加载特定的 [Products] 数组??
谢谢!
【问题讨论】:
-
您在调试器输出中看到了什么?
-
@SinaKH 致命错误:在展开可选值时意外发现 nil 2017-01-30 14:25:38.928171 Aromania[19538:6823926] 致命错误:在展开可选值时意外发现 nil 这是因为在
print我只有manPerfumeData。我如何识别在哪个集合视图中按下了按钮?
标签: ios swift swift3 uicollectionview