删除单元格后更新 tableView 标题中的 UILabel
update UILabel in tableViewHeader after cell is deleted
我在 tableViewHeader 中有一个 UILabel,它显示 tableView 的行计数值。删除一行后,我想在删除一行后立即更新 tableViewHeader UILabel 中的计数值(所以不是在用户转到不同的 VC 和 returns 回到 tableView 之后然后刷新计数值)。同样,一旦从 tableView 中删除单元格,我希望 UILabel 值在同一视图中更新。感谢您的帮助!
// tableViewHeader class
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)
}
}
试试这个:
placeList.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .left)
tableView.reloadSections(IndexSet(integer: indexPath.section), with: .automatic)
tableView.endUpdates()
我在 tableViewHeader 中有一个 UILabel,它显示 tableView 的行计数值。删除一行后,我想在删除一行后立即更新 tableViewHeader UILabel 中的计数值(所以不是在用户转到不同的 VC 和 returns 回到 tableView 之后然后刷新计数值)。同样,一旦从 tableView 中删除单元格,我希望 UILabel 值在同一视图中更新。感谢您的帮助!
// tableViewHeader class
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)
}
}
试试这个:
placeList.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .left)
tableView.reloadSections(IndexSet(integer: indexPath.section), with: .automatic)
tableView.endUpdates()