2017-06-14 71 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其他单元格将根据这些复选框进行更新。 –