Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Swift 当我向下滚动并快速返回时,Tableview单元格设置保持更改_Swift - Fatal编程技术网

Swift 当我向下滚动并快速返回时,Tableview单元格设置保持更改

Swift 当我向下滚动并快速返回时,Tableview单元格设置保持更改,swift,Swift,我正在尝试创建一个类似于图(1)的表视图。当表格视图单元格的文本获取“取消计划”或“休息日”时,按钮“>”将为灰色,单元格无法单击 我的代码正常工作,但当我向下滚动并返回时,设置将混乱。设置将随机更改,如图(2)和(3)所示。为什么? 图(1): 类似于图(2)或图(3)的东西 图(2): 图(3):原因是细胞重复使用。 这意味着已经以某种方式设置了样式的单元格可以重用以显示不同的数据。每次重复使用单元格时,必须确保设置样式的各个方面,以避免交叉 如果您使用configureCell方法来处理样

我正在尝试创建一个类似于图(1)的表视图。当表格视图单元格的文本获取“取消计划”或“休息日”时,按钮“>”将为灰色,单元格无法单击

我的代码正常工作,但当我向下滚动并返回时,设置将混乱。设置将随机更改,如图(2)和(3)所示。为什么?

图(1):

类似于图(2)或图(3)的东西

图(2):

图(3):

原因是细胞重复使用。 这意味着已经以某种方式设置了样式的单元格可以重用以显示不同的数据。每次重复使用单元格时,必须确保设置样式的各个方面,以避免交叉

如果您使用
configureCell
方法来处理样式,可能更容易跟踪样式,这样您就不会忘记设置一个值,甚至可以设置一些默认值。

func configureCell(cell:RosterCell,
               textColour:UIColor,
               seperatorColour:UIColor,
               buttonEnabled:Bool,
               buttonAlpha:CGFloat,
               interactionEnabled:Bool = false) {

        cell.separator.backgroundColor = seperatorColour
        cell.jobDetail.textColor = textColour
        cell.button.isEnabled = buttonEnabled
        cell.button.alpha = buttonAlpha
        cell.isUserInteractionEnabled = interactionEnabled

}
然后您的
cellForRowAt
方法将如下所示

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "rosterId", for: indexPath) as! RosterCell
    ...
    if jobCancel[indexPath.section] == 0{
        configureCell(cell: cell,
                  textColour: UIColor.gray,
                  seperatorColour: UIColor.darkGray,
                  buttonEnabled: false,
                  buttonAlpha: 0.3,
                  interactionEnabled: true)
    }
    if cell.jobDetail.text == "Cancelled Schedule"{
        configureCell(cell: cell,
                  textColour: UIColor.gray,
                  seperatorColour: UIColor.red,
                  buttonEnabled: false,
                  buttonAlpha: 0.3)
    }
    if cell.jobDetail.text == "Off Day"{
        configureCell(cell: cell,
                  textColour: UIColor.gray,
                  seperatorColour: UIColor.darkGray,
                  buttonEnabled: false,
                  buttonAlpha: 0.3)
    }

}
如您所见,我已将
interactionEnabled
参数设置为默认值
false
,因此我不需要为最后两个if语句设置它。

原因是单元重用。 这意味着已经以某种方式设置了样式的单元格可以重用以显示不同的数据。每次重复使用单元格时,必须确保设置样式的各个方面,以避免交叉

如果您使用
configureCell
方法来处理样式,可能更容易跟踪样式,这样您就不会忘记设置一个值,甚至可以设置一些默认值。

func configureCell(cell:RosterCell,
               textColour:UIColor,
               seperatorColour:UIColor,
               buttonEnabled:Bool,
               buttonAlpha:CGFloat,
               interactionEnabled:Bool = false) {

        cell.separator.backgroundColor = seperatorColour
        cell.jobDetail.textColor = textColour
        cell.button.isEnabled = buttonEnabled
        cell.button.alpha = buttonAlpha
        cell.isUserInteractionEnabled = interactionEnabled

}
然后您的
cellForRowAt
方法将如下所示

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "rosterId", for: indexPath) as! RosterCell
    ...
    if jobCancel[indexPath.section] == 0{
        configureCell(cell: cell,
                  textColour: UIColor.gray,
                  seperatorColour: UIColor.darkGray,
                  buttonEnabled: false,
                  buttonAlpha: 0.3,
                  interactionEnabled: true)
    }
    if cell.jobDetail.text == "Cancelled Schedule"{
        configureCell(cell: cell,
                  textColour: UIColor.gray,
                  seperatorColour: UIColor.red,
                  buttonEnabled: false,
                  buttonAlpha: 0.3)
    }
    if cell.jobDetail.text == "Off Day"{
        configureCell(cell: cell,
                  textColour: UIColor.gray,
                  seperatorColour: UIColor.darkGray,
                  buttonEnabled: false,
                  buttonAlpha: 0.3)
    }

}

如您所见,我已将
interactionEnabled
参数设置为默认值
false
,因此我不需要为最后两个if语句设置该参数。

当您返回到同一单元格时,单元格将被重新创建,如果您根据单元格类中的某个属性值做出决策,请确保属性将被重新初始化,丢失您设置的值。当您返回到同一单元格时,单元格将被重新创建。如果您根据单元格类中的某个属性值进行决策,请确保属性将被重新初始化,从而丢失您设置的值。