2016-11-24 72 views
0

我的按钮正常工作,我无法弄清楚如何在水龙头上禁用它。我不确定是否可以从addSomething(sender:UIButton)函数引用它,例如我引用sender.tag。 有什么想法?谢谢你的帮助。在swift中禁用水龙头上的tableview单元按钮

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

你有什么需要做的是存储所有按键敲击成一排的检查此标签(目前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() 
     // ... 
    } 
} 

我希望这帮助。

+0

感谢您的全力以赴,但放在适当的位置后,它似乎只做你的检查时,tableview加载不是单击按钮之后。 – user3712837

+0

它很简单,答案已经通过在按钮的目标中添加“tableView.reloadData()”进行编辑:) –

+0

啊,谢谢!这很好用!我也循环查看数据库之前已经添加的数组,并且他们工作得很好。 – user3712837

相关问题