2016-11-24 81 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

相關問題