【问题标题】:swift didSelectRowAtIndexPath can be called several timesswift didSelectRowAtIndexPath 可以多次调用
【发布时间】:2015-03-15 02:42:03
【问题描述】:

我的表格视图有问题,我可以很好地点击我的单元格。但是如果我快速点击同一个单元格两次。它运行“didSelectRowAtIndexPath”两次。在我的导航控制器堆栈中添加 2 个相同的视图

函数如下:

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

    var VC = detaiLSuggestion_VC()
    self.navigationController?.pushViewController(VC, animated: true)

    var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

    if selectedCell.backgroundColor == UIColor.formulaFormColor() {
        println("Buy Action")
        selectedCell.backgroundColor = UIColor.formulaMediumBlue()
        UIView.animateWithDuration(0.5, animations: {
            selectedCell.backgroundColor = UIColor.formulaFormColor()
        })
    } else {
        println("Sell Action")
        selectedCell.backgroundColor = UIColor.formulaGreenColor()
        UIView.animateWithDuration(0.5, animations: {
            selectedCell.backgroundColor = UIColor.formulaLightGreenColor()
        })
    }

为什么我可以选择同一个单元格两次?我将如何解决这个问题?

【问题讨论】:

    标签: ios uitableview swift didselectrowatindexpath tableviewcell


    【解决方案1】:

    为什么我可以选择同一个单元格两次?

    因为单元格仍在屏幕上待点按。您可能想做一些除了通过单元格点击导航之外的事情,然后您不想阻止未来的点击。如果不是这种情况,多选也会复杂得多。

    我该如何解决这个问题?

    一个简单的布尔标志来指示某些东西已被点击,否则你可能想在呈现另一个视图控制器时将 CollectionView 的 allowsSelection 属性设置为 NO

    【讨论】:

    • "tableView.allowsSelection = false" 效果很好,感谢 Tristan 的帮助!
    • 哈哈,我刚刚意识到您在谈论表视图而不是集合视图。我猜所有的都一样。
    • 有时 iOS 很糟糕。如果选择了一个单元格,人们会期望无法再次选择它。如果每次用户点击单元格时都会触发该方法,则应将其命名为 didTapCell
    猜你喜欢
    • 2015-05-17
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2013-10-12
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    相关资源
    最近更新 更多