我正在尝试重新加载swift 4中按钮单击的特定部分。为此,我使用以下代码:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView:CustomCell = tableView.dequeueReusableCell(withIdentifier: "reuseCell") as! CustomCell
let dict:[String:Any]=array[section]
let firstKey = Array(dict.keys)[0]
headerView.categoryLabel.text=firstKey
headerView.categoryLabel.textColor = UIColor.black
currentSection=section
print("value of section and current section here %@%@",section,currentSection)
let tapRecognizer = UIButton()
tapRecognizer.frame = CGRect.init(x: 0, y:10, width:self.tableView.frame.size.width, height:50)
//tapRecognizer.backgroundColor=UIColor.red
tapRecognizer.tag=section
tapRecognizer.addTarget(self, action: #selector(tapOnHeaderView(_sender:)), for: .touchUpInside)
headerView.addSubview(tapRecognizer)
return headerView;
}
@objc func tapOnHeaderView(_sender:UIButton){
print("value of sender tag","\(_sender.tag)")
onReload = true
print("value of index vala tag",IndexSet(integer: _sender.tag))
let sectionToReload = 0
let indexSet: IndexSet = [sectionToReload]
self.tableView.reloadSections(indexSet, with: .automatic)
}
extension ViewController:UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(indexPath.section==0){
if(!onReload){
return 0
}else{
return 40
}}
else{
if(!onReload){
return 0
}else{
return 40
}
}
}
当我点击任何按钮时,预计只有第0部分会重新加载。但是,实际上当我点击按钮部分0和第1部分时都会重新加载。请帮我解决这个问题。