UICollectionViewCell 在第一次点击时取消选择预先选择的单元格
UICollectionViewCell Deselect Pre-Selected Cell on First Tap
我正在模态地展示一个 viewcontroller
,它承载一个 viewcollection
,其中 "pre-selected" 单元格根据在 segue 上传递的数据格式化为不同的背景颜色。
当我点击其中一个 "pre-selected" 单元格时,需要点击两次才能触发 didDeselectItemAt
委托方法。我理解为什么在调试时会发生这种情况,虽然不同颜色的单元格不一定在选定状态下被识别。 有什么方法可以先触发 "pre-selected" 个单元格的 didDeselectItemAt
吗?
我已经尝试在委托方法 cellForItemAt
中合并设置 cell.isSelected = true
作为更改单元格背景颜色的条件语句的一部分。类似地,在同一个委托方法中,我还尝试调用一个函数,该函数将使用这些 "pre-selected" 单元格的 indexPaths 调用委托方法 didSelectItemAt
。两者产生相同的结果。
以下是(缩写的)相关代码片段:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! ExampleCollectionViewCell
if preselectedDataPoints { cell.backgroundColor = blue }
else { cell.backgroundColor = white }
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = blue
preselectedDataPoints.append(newDataPoint)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = white
preselectedDataPoints.remove(at: existingDataPoint)
}
如果预选单元格,则在 didSelectItem 中以编程方式调用 collectionView.deselectItem(at: indexPath, animated: true)
。
推荐人代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if preselectedDataPoints {
collectionView.deselectItem(at: indexPath, animated: true)
}else{
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = blue
preselectedDataPoints.append(newDataPoint)
}
}
或
在 deSelect 方法中直接调用您需要执行的任何代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if preselectedDataPoints {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = white
preselectedDataPoints.remove(at: existingDataPoint)
}else{
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = blue
preselectedDataPoints.append(newDataPoint)
}
}
我正在模态地展示一个 viewcontroller
,它承载一个 viewcollection
,其中 "pre-selected" 单元格根据在 segue 上传递的数据格式化为不同的背景颜色。
当我点击其中一个 "pre-selected" 单元格时,需要点击两次才能触发 didDeselectItemAt
委托方法。我理解为什么在调试时会发生这种情况,虽然不同颜色的单元格不一定在选定状态下被识别。 有什么方法可以先触发 "pre-selected" 个单元格的 didDeselectItemAt
吗?
我已经尝试在委托方法 cellForItemAt
中合并设置 cell.isSelected = true
作为更改单元格背景颜色的条件语句的一部分。类似地,在同一个委托方法中,我还尝试调用一个函数,该函数将使用这些 "pre-selected" 单元格的 indexPaths 调用委托方法 didSelectItemAt
。两者产生相同的结果。
以下是(缩写的)相关代码片段:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! ExampleCollectionViewCell
if preselectedDataPoints { cell.backgroundColor = blue }
else { cell.backgroundColor = white }
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = blue
preselectedDataPoints.append(newDataPoint)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = white
preselectedDataPoints.remove(at: existingDataPoint)
}
如果预选单元格,则在 didSelectItem 中以编程方式调用 collectionView.deselectItem(at: indexPath, animated: true)
。
推荐人代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if preselectedDataPoints {
collectionView.deselectItem(at: indexPath, animated: true)
}else{
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = blue
preselectedDataPoints.append(newDataPoint)
}
}
或
在 deSelect 方法中直接调用您需要执行的任何代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if preselectedDataPoints {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = white
preselectedDataPoints.remove(at: existingDataPoint)
}else{
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = blue
preselectedDataPoints.append(newDataPoint)
}
}