在Swift中禁用TableView单元按钮



我的按钮正常工作,我只是无法弄清楚如何在Tap上禁用它。我不确定是否可以像我引用sender.tag一样从addsomething(发件人:uibutton)函数引用它。任何想法?感谢您的任何帮助。

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
}

您需要做什么是将所有点击按钮存储在数组中,以检查该标签的按钮(当前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()
        // ...
    }
}

我希望这有所帮助。

最新更新