【发布时间】:2017-06-14 04:48:37
【问题描述】:
我有一个 tableView 有一个两个单元格。
第一个用于具有 3 个按钮的节标题,用作复选框,并且
带有简单标签的第二个单元格用于填充数据。
现在我想要更新 tableView 的第二个单元格数据,其标题如下面的屏幕截图所示。但我无法在同一个标题上获得这些按钮的可点击操作。
到目前为止我尝试的是:
-
首先我对它们三个都使用了 tapReconizer,它正在工作,但它没有改变按钮的图像(这是小鬼,因为通过图像它就像一个复选框)
李> -
然后我为所有三个创建了动作出口,现在它们正在工作,但我无法从单元格的自定义类中更新数据,下面是代码
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