Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewCell在第一次点击时取消选择预选单元格_Ios_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionViewCell在第一次点击时取消选择预选单元格

Ios UICollectionViewCell在第一次点击时取消选择预选单元格,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我现在演示一个viewcontroller,它承载一个viewcollection,其中的“预选”单元格根据在segue上传递的数据格式化为不同的背景色 当我点击其中一个“预选”单元格时,需要点击两次才能触发DIDDeselectAtdelegate方法。我理解为什么在调试时会发生这种情况,虽然单元格颜色不同,但在选定状态下不一定能识别有没有办法先触发“预选”单元格的DIDDeselectAt 在委托方法cellForItemAt中,我尝试将设置cell.isSelected=true作为更改单

我现在演示一个
viewcontroller
,它承载一个
viewcollection
,其中的“预选”单元格根据在segue上传递的数据格式化为不同的背景色

当我点击其中一个“预选”单元格时,需要点击两次才能触发
DIDDeselectAt
delegate方法。我理解为什么在调试时会发生这种情况,虽然单元格颜色不同,但在选定状态下不一定能识别有没有办法先触发“预选”单元格的
DIDDeselectAt

在委托方法
cellForItemAt
中,我尝试将设置
cell.isSelected=true
作为更改单元格背景颜色的条件语句的一部分。类似地,在同一个委托方法中,我还尝试调用一个函数,该函数将调用委托方法
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.DecelectItem(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)
    }  

}

直接调用需要在取消选择方法中执行的代码

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)
    }  

}

如果单元格已预选,则以编程方式调用didSelectItem中的
collectionView.DecelectItem(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)
    }  

}

直接调用需要在取消选择方法中执行的代码

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)
    }  

}

在调用didSelectItem中的deSelect时,我收到了第一个建议错误,可能与所涉及的参数有冲突?第二个建议在第二次点击时调用了didDeselect委托方法,所以我仍然需要在其中维护代码。对我来说,解决这个问题的方法是在第二个建议的基础上进行构建,将所有条件语句组合起来,并在两个委托方法中使用它们。这样,不管触发了什么方法,都会执行相同的数据和逻辑并前后截击。我在调用didSelectItem中的取消选择时收到了一个错误,可能与所涉及的参数有冲突?第二个建议在第二次点击时调用了didDeselect委托方法,所以我仍然需要在其中维护代码。对我来说,解决这个问题的方法是在第二个建议的基础上进行构建,将所有条件语句组合起来,并在两个委托方法中使用它们。这样,不管触发了什么方法,都会执行相同的数据和逻辑,并前后截击。