【发布时间】:2017-08-09 11:39:08
【问题描述】:
我的项目中有一个 UICollectionView,我在其中添加每个单元格中添加的项目数量,并在 UIBarButtonItem 中显示总数。但是,当我滚动浏览视图时,数字会不断变化。我知道这个问题与集合和表视图重用单元格的事实有关,我对此做了很多研究,但我无法解决这个问题。覆盖“prepareForUse()”也无济于事。我仍然是初学者,如果我天真,请原谅。提前致谢。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = trayCell.dequeueReusableCell(withReuseIdentifier: "trayCell", for: indexPath) as! TrayItemCollectionViewCell
//initializing labels
if let quantity = arrayOfOrders[indexPath.row]["quantity"] as? Int{
totalQuantity += quantity
trayButton.title = "\(totalQuantity)"
}
return cell
}
【问题讨论】:
-
您试图在条形按钮标题中显示整个集合视图中的项目总数?
-
@Bader Yusuf Serhan 有区别不断变化还是重复?
-
请显示更多集合视图的代码
-
@jegadeesh 我的应用程序是餐厅菜单,您选择一组项目以及每个项目的数量,所以我试图显示总数量。它工作正常,但是当我向下或向上滚动时,值会随着单元格重新加载而不断变化
-
@TusharSharma 这个数字一直在增加,因为它每次都在增加
标签: ios xcode swift3 uicollectionview