簡體   English   中英

帶tableView按鈕的交互式標題

[英]Interactive header with buttons of 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
    }

在您的單元格類中創建像這樣的回調

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)
}

下面的代碼在您的單元格類中執行CallBack代碼之后執行

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

}

你必須做下面的事情,

  1. 代替UITableViewCell ,必須在ViewController使用Button Outlets。

  2. 當用戶單擊任何單元格時,您可以單擊哪個按鈕和單元格用戶。 參考

  3. 現在,在用戶單擊時重新加載該單元格/部分。 參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM