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 ios tableview在滚动时保持重新选择单元格_Ios_Swift_Uitableview_Tableview - Fatal编程技术网

带有自定义复选标记的swift ios tableview在滚动时保持重新选择单元格

带有自定义复选标记的swift ios tableview在滚动时保持重新选择单元格,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我有一个带有两个部分的tableview和一个在选择单元格时显示的自定义复选标记 唯一的问题是,如果我选择一个单元格并向下滚动,那么每当该单元格出现在屏幕上时,该单元格就会被重新选择,而所有其他单元格都会再次隐藏 那么我如何才能使一个单元格只被选中/取消选中一次呢 我的代码: //Keep track of selected row var selectedRow: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0) func

我有一个带有两个部分的tableview和一个在选择单元格时显示的自定义复选标记

唯一的问题是,如果我选择一个单元格并向下滚动,那么每当该单元格出现在屏幕上时,该单元格就会被重新选择,而所有其他单元格都会再次隐藏

那么我如何才能使一个单元格只被选中/取消选中一次呢

我的代码:

//Keep track of selected row
    var selectedRow: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0)

    func loadStates() {
        ApiService.getStates() { (JSON) -> () in
            self.states = JSON["stateData"]
            self.tableView.reloadData()
            let index = NSIndexPath(forRow: 0, inSection: 0)
            self.tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.Top)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        loadStates()
    }

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let paths:[NSIndexPath]

        if let previous = selectedRow {
            paths = [indexPath, previous]
        } else {
            paths = [indexPath]
        }
        selectedRow = indexPath
        tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: .None)

        if (indexPath.section == 1) {
            tableView.deselectRowAtIndexPath(indexPath, animated: false)
            performSegueWithIdentifier("searchCitySegue", sender: indexPath)
        }else {
            tableView.deselectRowAtIndexPath(indexPath, animated: false)
            dismissViewControllerAnimated(true, completion: nil)
        }

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 1){
            return states.count
        }
        return 1 
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell: searchStateTableViewCell!

        if (indexPath.section == 0) {
            cell = tableView.dequeueReusableCellWithIdentifier("staticStateCell") as! searchStateTableViewCell
            cell.titleLabel?.text = "All states"

            if indexPath == selectedRow {
               cell.stateImage.select()
            } else {
                cell.stateImage.deselect()
            }

        }else if (indexPath.section == 1) {
            cell = tableView.dequeueReusableCellWithIdentifier("stateCell") as! searchStateTableViewCell
            let state = states[indexPath.row]
            cell.configureWithStates(state)
            if indexPath == selectedRow {
                cell.stateImage.select()
            } else {
                cell.stateImage.deselect()
            }
        }

        return cell

    }

那么,是什么原因导致我的单元格每次在屏幕上显示选定动画时都会运行它呢?

我很确定这是因为
cellforrowatinedexpath
使用以下代码:

if indexPath == selectedRow {
    cell.stateImage.select()
}
else {
    cell.stateImage.deselect()
}
每次单元格出现时调用。尝试:

if indexPath == selectedRow {
    if !(cell.selected) {
        cell.stateImage.select()
    }
}
else {
    cell.stateImage.deselect()
}

我试图实现您的代码,但如果我向下滚动,然后再次向上滚动,单元格仍会被重新选择,以便在屏幕上可见:/@user2636197 Replace!(cell.selected)使用代码中的任何逻辑来确定单元格是否已处于选定状态。这样,如果是,它就不会再次尝试将其置于选定状态。我已经尝试了
if indexath==selectedRow{if!cell.stateImage.selected{cell.stateImage.selected()print(“select normal”)}else{if cell.stateImage.selected{cell.stateImage.deselect()print(“deselect normal”)}
一旦单元格从屏幕上滚下来,它就会被取消选中,然后又被选中。每次我滚回去/4次,看到select动画都会运行,这真的很烦人