在swift中点击禁用tableview单元格按钮

时间:2016-11-24 19:52:34

标签: swift uitableview tableviewcell

我按钮工作正常,我只是无法弄清楚如何禁用它。我不确定我是否可以从addSomething(sender:UIButton)函数引用它,就像我引用sender.tag一样。 任何的想法?谢谢你的帮助。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ExploreCell

    // Configure the cell...
    myCell.configureCell(teams[indexPath.row])

    myCell.addSomethingButton.tag = indexPath.row
    myCell.addSomethingButton.addTarget(self, action: #selector(self.addSomething), forControlEvents: .TouchUpInside)

    myCell.addSomethingButton.enabled = true

    //disable cell clicking
    myCell.selectionStyle = UITableViewCellSelectionStyle.None

    return myCell
}

1 个答案:

答案 0 :(得分:1)

您需要做的是将所有点按的按钮存储在一个数组中,以检查是否已点击此标记的按钮(当前indexPath.row):

class ViewController: UIViewController {
    var tappedButtonsTags = [Int]()

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ExploreCell

        // Configure the cell...
        myCell.configureCell(teams[indexPath.row])

        myCell.addSomethingButton.tag = indexPath.row

        // here is the check:
        if tappedButtonsTags.contains(indexPath.row) {
            myCell.addSomethingButton.enabled = false
        } else {
            myCell.addSomethingButton.addTarget(self, action: #selector(self.addSomething), forControlEvents: .TouchUpInside)
            myCell.addSomethingButton.enabled = true
        }

        //disable cell clicking
        myCell.selectionStyle = UITableViewCellSelectionStyle.None

        return myCell
    }

    // I just Implemented this for demonstration purposes, you can merge this one with yours :)
    func addSomething(button: UIButton) {
        tappedButtonsTags.append(button.tag)
        tableView.reloadData()
        // ...
    }
}

我希望这会有所帮助。