Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 单元格在向后滚动时不会被取消选择_Swift_Uitableview_Swift3 - Fatal编程技术网

Swift 单元格在向后滚动时不会被取消选择

Swift 单元格在向后滚动时不会被取消选择,swift,uitableview,swift3,Swift,Uitableview,Swift3,我有个奇怪的问题。当我通过更改一个UIView组件的背景颜色来选择一个单元格时,我向下滚动,所选单元格将超出视图的范围。我确实选择了新单元格->上一个单元格应该被取消选择,但它没有。因为当我回来时,我有两个选定的单元格 var lastSelectedAtIndexPath: IndexPath? func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { switch tabl

我有个奇怪的问题。当我通过更改一个UIView组件的背景颜色来选择一个单元格时,我向下滚动,所选单元格将超出视图的范围。我确实选择了新单元格->上一个单元格应该被取消选择,但它没有。因为当我回来时,我有两个选定的单元格

var lastSelectedAtIndexPath: IndexPath?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch tableView {
        case myTableView:

            if let cell = myTableView.cellForRow(at: indexPath) as? MyTableViewCell {
                cell.checkMarkBorder.backgroundColor = UIColor.darkGreyFts
                lastSelectedFuelAtIndexPath = indexPath
            }

        default:
            break
        }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        switch tableView {
        case myTableView:

            if let cell = fuelTableView.cellForRow(at: indexPath) as? MyTableViewCell {
                cell.checkMarkBorder.backgroundColor = UIColor.white
            }

        default:
            break
        }
    }
伊西德·塞尔福罗我有:

知道发生了什么事吗?
提前谢谢

不要在cellForRowAt之外的单元格上进行更改始终更改数据源,并在cellForRowAt中使用数据源,然后在didSelectRowAt和DidDeclesRowat中重新加载行

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! MyTableViewCell
    cell.myImage.image = UIImage(named: fuelType)?.withRenderingMode(.alwaysTemplate)
    cell.myNameLabel.text = "Test"
    cell.checkMarkBorder.backgroundColor = lastSelectedFuelAtIndexPath == indexPath ? UIColor.darkGreyFts : UIColor.white
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == myTableView {           
        lastSelectedFuelAtIndexPath = indexPath
        myTableView.reloadRows(at: [indexPath], with: .automatic)
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if tableView == myTableView {
        lastSelectedFuelAtIndexPath = nil
        myTableView.reloadRows(at: [indexPath], with: .automatic)
    }
}
现在在didSelectRowAt和didSelectRowAt中更新最后选择的FuelAtIndex路径,重新加载行

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! MyTableViewCell
    cell.myImage.image = UIImage(named: fuelType)?.withRenderingMode(.alwaysTemplate)
    cell.myNameLabel.text = "Test"
    cell.checkMarkBorder.backgroundColor = lastSelectedFuelAtIndexPath == indexPath ? UIColor.darkGreyFts : UIColor.white
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == myTableView {           
        lastSelectedFuelAtIndexPath = indexPath
        myTableView.reloadRows(at: [indexPath], with: .automatic)
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if tableView == myTableView {
        lastSelectedFuelAtIndexPath = nil
        myTableView.reloadRows(at: [indexPath], with: .automatic)
    }
}

你有多个tableView吗?@NiravD是的,我有。最大的问题是,如果我在索引路径行=0处选择单元格,然后向下滚动-要选择新单元格,我必须点击2次。显示cellForRowAt方法中的更多代码,以及选择已选择的单元格时会发生什么?它是否需要取消选择?'cellForRowAt'只需要选择两个组件。一个是向图像视图添加图像,第二个是向标签添加文本。基本上没有别的了。我会更新这个问题。我想选择项目,然后在选择新项目后,取消选择旧项目并选择新项目。我明白了。但是,当您选择已选择的单元格时,会发生什么情况?是否需要取消选择?当我们向下滚动时,需要双击以选择新单元格,而上一个选定的单元格超出范围?因为它只在上一个选定单元格超出视图范围时发生。@yerpy请先尝试此解决方案,它肯定会解决双击的问题,而且您面临的问题是因为cellOK的重复使用,谢谢您的帮助。@yerpy欢迎您的朋友: