【问题标题】:How to remove custom view from invisible cell in collection view如何从集合视图中的不可见单元格中删除自定义视图
【发布时间】:2018-05-21 11:39:53
【问题描述】:

我正在尝试更改集合视图中选定自定义视图的颜色。我正在使用相同的集合视图的 didSelectItem 和 didDeselectItem 方法。

当我要更改 collectionview 中的不可见单元格时,我遇到了错误。

集合视图的可见单元格工作正常

我的代码在下面

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = colCellSize.cellForItem(at: indexPath) as! colSizeDetails
    cell.viewDetails.backgroundColor = UIColor.gray
    cell.lblSize.textColor = UIColor.white
}


func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    let cell = colCellSize.cellForItem(at: indexPath) as! colSizeDetails
    cell.viewDetails.layer.borderWidth = 2.0
    cell.viewDetails.layer.borderColor = UIColor.gray.cgColor
    cell.lblSize.textColor = UIColor.gray
    cell.viewDetails.backgroundColor = UIColor.white
}

【问题讨论】:

  • 错误说明了什么?请发布错误日志
  • 线程 1:致命错误:在展开可选值时意外发现 nil -- 不显示错误日志。获取零索引路径
  • 该错误表明您正在解开一个初始值为 nil 的可选值。您没有提供初始化不可见单元格的位置以及取消它的时间。请提供更多代码sn-ps。

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

这一行是错误的:

let cell = colCellSize.cellForItem(at: indexPath) as! colSizeDetails

在这里,您将cell 转换为colSizeDetailscolSizeDetails 应该是它自己的类,具有UICollectionViewCell 的子类。你的类名不应该以小写字母开头。您需要将您的班级名称更改为ColSizeDetails,然后用大写字母重写该行。这可能是问题,因为我看不到你强制解包选项

【讨论】:

  • colSizeDetails 是 collectionview 的名称。我用它来配置 collectionview 的自定义单元格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多