2017-06-14 39 views
0

我有一個tableView有一個兩個單元格。交互式標題與按鈕的tableView

  1. 第一對於具有3個按鈕節頭,充當複選框,

  2. 用簡單的標籤第二小區來填充數據。

現在我想要的是更新tableView的第二個單元格數據的部分標題,如下面的截圖所示。但是我無法在同一個標​​題上獲得這些按鈕的可點擊動作。

我試過到目前爲止:

  1. 首先我用tapReconizer所有他們三個,這是工作,但它並沒有改變按鈕的圖像(這是小鬼,通過圖像它是像個複選框)

  2. 然後我做了所有三個現在他們正在爲但我無法更新從細胞的自定義類數據的動作出口,下面是代碼

    class SavedCallHeader : UITableViewCell{ 
    var checkBox_PlannedisOn:Bool = false 
    var checkBox_BothisOn:Bool = true 
    var checkBox_unPlannedisOn:Bool = false 
        let defaults = UserDefaults.standard 
    
    @IBOutlet weak var PlannedBoxBtn: UIButton! 
    @IBOutlet weak var BothBoxBtn: UIButton! 
    @IBOutlet weak var unPlannedBoxBtn: UIButton! 
    
    override func awakeFromNib() { 
        super.awakeFromNib() 
    } 
    
    
    @IBAction func PlannedCheckBox(_ sender: UIButton) { 
        if checkBox_PlannedisOn == false { 
         self.PlannedBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal) 
         checkBox_PlannedisOn = true 
         print("i'm finally here proper click!") 
    
        // self.fetchData() // wont' work here as it is in the main VC Class 
        // tableView.reloadData() // won't work here as well 
    
        }else { 
         self.PlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal) 
         print("i'm finally heress proper click!") 
         checkBox_PlannedisOn = false 
    
        } 
    
    } 
    

我想在每次用戶選擇/取消選擇複選框時更新和刷新數據。下面是我的主要代碼:

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

      let identifierHeader:String = "SavedCallHeader" 

      let headerCell = tableView.dequeueReusableCell(withIdentifier: identifierHeader) as! SavedCallHeader 



      /* let tapPlanned = UITapGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureP)) 
      let tapBoth = UITapGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureB)) 
      let tapUnPlanned = UITapGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureU)) 
      headerCell.PlannedBoxBtn.addGestureRecognizer(tapPlanned) 
      headerCell.BothBoxBtn.addGestureRecognizer(tapBoth) 
      headerCell.unPlannedBoxBtn.addGestureRecognizer(tapUnPlanned) 
    */ 
      return headerCell 

     } 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let identifier:String = "savedcall_cell" 

     let cell:SavedCalls_Cell = self.tableView.dequeueReusableCell(withIdentifier:identifier) as! SavedCalls_Cell! 
     if(drname.count > 0){ 
     //Fetching data from table view and then reload 
    } 
+0

不要使用手勢執行下面的代碼執行創建回調是這樣的。首先確保您的按鈕操作正確無誤。 –

+0

使用回電更新您的主代碼 – Bala

+0

@dahiya_boy是的,它正在調用正確,因爲我提到,在按鈕點擊圖像越來越chenged。但問題是我無法更新該操作的數據點擊,因爲它在自定義單元格的類中。 –

回答

2

在你的類

var callBackForReload : ((Bool) ->())? 

@IBAction func PlannedCheckBox(_ sender: UIButton) { 
    // when you call this call back it will excute in where you acces it. 
    // I pass bool value for examble. you will pass whtever datatype you want 
    self.callBackForReload!(true) 
} 

回調代碼後,你的類

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let identifierHeader:String = "SavedCallHeader" 

    let headerCell = tableView.dequeueReusableCell(withIdentifier: identifierHeader) as! SavedCallHeader 

    headerCell.callBackForReload = { [weak self] (isCalled) -> Void in 
     //This will call when the call back code excuted in your cell class 
     // in The isCalled variable you will get the value from cell class 
     // You will reload your changed value here 
    } 

    return headerCell 

} 
+0

它顯示headerCell.contentHeight錯誤,因爲沒有contentHeight字段!? –

+0

對不起現在檢查 – Bala

+0

工作!謝謝Alottt! –

0

你要做的事情如下,

  1. 相反的UITableViewCell,你必須採取按鈕奧特萊斯ViewController

  2. 當用戶點擊任何單元格,你可以得到哪些按鈕和單元格用戶點擊。 Ref

  3. 現在重新加載用戶單擊的單元格/部分。 Ref

+0

我想通過標題實現這個! –

+0

@shahtajkhalid這三個選項(計劃/未計劃/兩者)是針對每個單元的? –

+0

它是tableview的總體標題,yup其他單元格將根據這些複選框進行更新。 –

相關問題