首页 > 解决方案 > 如何在每次点击更新的条件下禁用特定 CollectionViewCell 的用户交互

问题描述

每个单元格都是一个结构,其中包含一个变量:numberOfRegistration。

如果用户单击该单元格,则 numberOfRegistration 将 +1。

如果 counter = 5 或更多,则单元格的用户交互将被禁用。

我以某种方式想出了这个,但它没有用。

struct hourSlot{
    var hoursService:String
    var numberOfRegistration:Int = 0
    var isDisable:Bool = false
    mutating func disable(){
        if numberOfRegistration >= 5{
            isDisable = true
        }
    }
}

在 cellForItemAt 中:

var isDisableCell = hoursArray[indexPath.item].isDisable
if isDisableCell{
    cell.isUserInteractionEnabled = false
}

注意: hoursArray 是一个包含结构的数组。

标签: iosswiftuicollectionview

解决方案


尝试这个:

let hourSlot = hoursArray[indexPath.item]
hourSlot.disable()
if hourSlot.isDisable{
    cell.isUserInteractionEnabled = false
}

为了改变 isDisable 你需要调用 disable() 方法。然后你可以检查它的变量。


推荐阅读