【问题标题】:Interactive header with buttons of tableView带有 tableView 按钮的交互式标题
【发布时间】:2017-06-14 04:48:37
【问题描述】:

我有一个 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
    }

【问题讨论】:

  • 不要使用手势。首先确保您的按钮操作是否正确调用。
  • 使用回调来更新你的主代码
  • @dahiya_boy 是的,正如我所提到的,它被正确调用了,按钮上的单击图像正在生成。但问题是我无法从该动作点击更新数据,因为它在自定义单元格的类中。
  • @Bala 怎么样?我对 iOS 有点陌生。
  • 使用委托与你的主类交互

标签: ios swift uitableview callback


【解决方案1】:

在你的单元格类中像这样创建回调

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

}

【讨论】:

  • headerCell.contentHeight 显示错误,因为没有 contentHeight 字段!?
  • 抱歉现在查看
  • 欢迎... :-)
  • 我又遇到了一个小问题,希望你能解决它,正如我提到的 3 个按钮,我使用了你的回调 3 次,并在 isCalled 上应用条件,一切正常。但是当它尝试获取数据并重新加载它时,它没有访问两个按钮的 actionOutlet,只有一个按钮在工作。我不知道为什么只有重新加载数据会导致这种情况!
  • 我刚刚重新检查,它正在访问动作插座,但没有改变条件的值以及图像。
【解决方案2】:

你必须做以下事情,

  1. 您必须使用ViewController 中的按钮插座,而不是UITableViewCell

  2. 当用户单击任何单元格时,您可以获得用户单击的按钮和单元格。 Ref

  3. 现在在用户单击时重新加载该单元格/部分。 Ref

【讨论】:

  • 我想通过header来实现!!
  • @shahtajkhalid 这三个选项(计划/计划外/两者)是否适用于每个单元格??
  • 它是表格视图的整体标题,是的,每隔一个单元格将根据这些复选框进行更新。
猜你喜欢
  • 2017-10-02
  • 2017-02-05
  • 2015-04-11
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多