gpt4 book ai didi

ios - 带有 tableView 按钮的交互式标题

转载 作者:行者123 更新时间:2023-11-28 12:21:53 26 4
gpt4 key购买 nike

我有一个 tableView,它有一个两个单元格。

  1. 首先是具有 3 个按钮的节标题,充当复选框,以及

  2. 用于填充数据的带有简单标签的第二个单元格。

现在我想要的是使用节标题更新 tableView 的第二个单元格数据,如下面的屏幕截图所示。但是我无法在同一个标​​题上获得这些按钮的可点击操作。

到目前为止我尝试的是:

  1. 首先,我对所有三个按钮都使用了 tapReconizer,它可以正常工作,但它没有更改按钮的图像(这是 imp,因为通过图像它就像一个复选框)

    <
  2. 然后我为所有三个都创建了 action outlet,现在它们正常工作,但我无法从单元格的自定义类更新数据,下面是代码

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

在您的单元类中执行回调代码后执行以下代码

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

}

关于ios - 带有 tableView 按钮的交互式标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44535660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com