【问题标题】:swift ios tableview with custom checkmark keeps reselecting cell when scrolling带有自定义复选标记的swift ios uitableview在滚动时不断取消选择单元格
【发布时间】:2016-08-07 16:05:22
【问题描述】:

我有一个包含两个部分的表格视图和一个在选择单元格时显示的自定义复选标记。

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

那么我怎样才能让一个单元格只被选中/取消选中一次。

我的代码:

//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

    }

那么,是什么导致我的单元格在每次重新出现在屏幕上时运行选定的动画?

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    我很确定这是因为cellForRowAtIndexPath 使用此代码:

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

    每次出现单元格时都会被调用。试试:

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

    【讨论】:

    • 我试图实现你的代码,但如果我向下滚动然后再次向上滚动,单元格仍然会被重新选择,因此它会在屏幕上可见:/
    • @user2636197 将 !(cell.selected) 替换为代码中确定单元格是否已处于选定状态的任何逻辑。这样,如果是,它就不会再次尝试将其置于选定状态。
    • 我已经尝试过了if indexPath == selectedRow { if !cell.stateImage.selected { cell.stateImage.select() print("select normal") } } else { if cell.stateImage.selected { cell.stateImage.deselect() print("deselect normal") } } 一旦单元格被滚动出屏幕并再次返回,它就会被取消选择然后再次被选中。每次我向后/第四次滚动时看到选择动画运行真的很烦人
    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2011-04-27
    • 2021-10-20
    相关资源
    最近更新 更多