2016-09-02 20 views
1

我在一個tableView有一個測驗,有4個按鈕(選項),我把它們標記在201,202,203,204這樣的故事板上,並且在tableView方法中成功地獲得了它們。但是在向按鈕添加目標之後,我無法在buttonClicked方法中獲得特定的按鈕。如何從單個tableViewcell獲取多個按鈕?

func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return questions.count } 

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

    (cell.viewWithTag(100) as! UILabel).text = "Q : " + (questions[indexPath.row].objectForKey("MocQuestion")! as? String)! 
    (cell.viewWithTag(100) as! UILabel).font = themeFont 
    (cell.viewWithTag(101) as! UILabel).text = questions[indexPath.row].objectForKey("Op1")! as? String 
    (cell.viewWithTag(102) as! UILabel).text = questions[indexPath.row].objectForKey("Op2")! as? String 
    (cell.viewWithTag(103) as! UILabel).text = questions[indexPath.row].objectForKey("Op3")! as? String 
    (cell.viewWithTag(104) as! UILabel).text = questions[indexPath.row].objectForKey("Op4")! as? String 

    let btn1 = (cell.viewWithTag(201) as! UIButton) 
    let btn2 = (cell.viewWithTag(202) as! UIButton) 
    let btn3 = (cell.viewWithTag(203) as! UIButton) 
    let btn4 = (cell.viewWithTag(204) as! UIButton) 


//  btn1.tag = indexPath.row * 100 + 0 
//  btn1.tag = indexPath.row * 100 + 1 
//  btn1.tag = indexPath.row * 100 + 2 
//  btn1.tag = indexPath.row * 100 + 3 


    btn1.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside) 
    btn2.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside) 
    btn3.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside) 
    btn4.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside) 

    return cell 
} 

func buttonClicked(sender:UIButton) 
{ 
    let tag = sender.tag 
    print(tag) 
} 

enter image description here

+0

那麼是什麼問題?你的標籤應該打印perfact。你應該有問題來確定正確的行! – Lion

回答

1

如果你想indexPath訪問問題Array那麼你可以嘗試這樣的。

func buttonClicked(sender:UIButton) { 
    let center = sender.center 
    let point = sender.superview!.convertPoint(center, toView:self.tableView) 
    let indexPath = self.tableView.indexPathForRowAtPoint(point) 
    //Now you have tag of button check for that 
    if (sender.tag == 201) { 
     print("Option A") 
    } 
    else if (sender.tag == 202) { 
     print("Option B") 
    } 
    else if (sender.tag == 203) { 
     print("Option C") 
    } 
    else { 
     print("Option D") 
    } 
    print(question[indexPath.row]) 
} 
+0

但選項A的問題?另外我還需要獲得其他3個按鈕進行更改。但對於特定的問題。 –

+0

for'(questions [indexPath.row] .objectForKey(「MocQuestion」)'我想這個問題!! – Lion

+0

仔細閱讀我的答案,它會給你indexPath,從中你可以得到哪個按鈕點擊的問題。需要更改'cellForRowAtIndexPath'中的標籤。 –

0

爲了讓每一個疑問句單獨的按鈕click event你可以通過唯一的ID作爲後綴或前綴e.g 20101或每疑問句的01201作爲按鈕的標籤,而不是硬編碼。然後獲得標籤並首先提取問題標識,然後按照問題進行檢查。

0

SWIFT 3你可以嘗試波紋一樣在表格視圖

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Custom_Cell; 



    cell.ButtonA.tag = indexPath.row; 
    cell.ButtonB.tag = indexPath.row; 
    cell.ButtonA.tag = indexPath.row; 

    //Add Action Methods to UIButtons 
    cell.ButtonA.addTarget(self, action: #selector(ButtonAAction), for: .touchUpInside) 
    cell.ButtonB.addTarget(self, action: #selector(ButtonBAction), for: .touchUpInside) 
    cell.ButtonA.addTarget(self, action: #selector(ButtonCAction), for: .touchUpInside) 

    return cell; 
} 
  • 按鍵操作看起來

    1. 我的表格單元格類

      class Custom_Cell: UITableViewCell {  
      
          @IBOutlet weak var ButtonA: UIButton! 
          @IBOutlet weak var ButtonB: UIButton! 
          @IBOutlet weak var ButtonC: UIButton! 
      
      } 
      
    2. 設置標籤像..

      func ButtonAAction(_ sender: Any) { 
          //Get Button cell position. 
          let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableView) 
          let indexPath = tableView.indexPathForRow(at: ButtonPosition) 
          if indexPath != nil { 
      
      
           print("Cell indexPath: \(indexPath?.row)") 
          } 
          }