我有一个tableView,它有两个单元格。
首先是有3个按钮的节标题,作为复选框,
带有简单标签的第二个单元格,用于填充数据。
现在我想要的是用截面标题更新tableView的第二个单元格数据,如下面的截图所示。但我无法在同一标题上获取这些按钮的可点击操作。
到目前为止我尝试的是:
首先我将tapReconizer用于所有这三个,它正在工作,但它没有改变按钮的图像(这是imp,因为通过图像它就像一个复选框)
然后我为这三个人制作了动作插座现在他们正在工作,但我无法更新来自单元格自定义类的数据,下面是代码
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
}
}
我想在每次用户选择/取消选中checkBox时更新和刷新数据。以下是我的主要代码:
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 :(得分:2)
在您的单元格类中创建这样的callBack
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 :(得分:0)