我在tableViewHeader中有一个UILabel,它显示tableView的行计数值。删除一行后,我想在删除一行后立即更新tableViewHeader UILabel中的计数值(因此,在用户转到另一个VC并返回到其后计数值被刷新的tableView之后)。同样,一旦从tableView中删除单元格,我希望UILabel值在同一视图中更新。谢谢你的帮助!
// tableViewHeader类
class Header: UITableViewHeaderFooterView {
var placeList = [Place]()
var placesDictionary = [String: Place]()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let nameLabel: UILabel = {
let label = UILabel()
// display the number of tableView rows in UILabel
label.text = "## of Places"
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.boldSystemFont(ofSize: 14)
return label
}()
func setupViews() {
addSubview(nameLabel)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
}
}
// headerView方法
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as? Header else { return nil }
view.nameLabel.text = "user has visited \(placeList.count) Places"
return view
}
//删除tableViewCell函数
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if let place = placeList[indexPath.row].place {
let uid = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference().child("users").child(uid!).child("Places")
ref.queryOrdered(byChild: "place").queryEqual(toValue: place).observe(.childAdded, with: { (snapshot) in
snapshot.ref.removeValue(completionBlock: { (error, reference) in
if error != nil {
print("There has been an error:\(String(describing: error))")
}
})
})
}
placeList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .left)
}
}
答案 0 :(得分:1)
试试这个:
placeList.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .left)
tableView.reloadSections(IndexSet(integer: indexPath.section), with: .automatic)
tableView.endUpdates()