gpt4 book ai didi

swift - 在 UILabel 中显示 tableView 计数

转载 作者:行者123 更新时间:2023-11-30 12:27:44 24 4
gpt4 key购买 nike

我想将 tableViewCells 的数量传递到 tableViewHeader UILabel 中。例如,标签应根据 tableView 中有多少 tableViewRow(基于 Firebase 数据)显示“There are ## Rows”。提前致谢!

//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]))
}
}

//tableViewController 类

    override func viewDidLoad() {
super.viewDidLoad()

tableView.register(Header.self, forHeaderFooterViewReuseIdentifier: "headerId")
tableView.sectionHeaderHeight = 50

ref = FIRDatabase.database().reference()
fetchPlaces()

placesClient = GMSPlacesClient.shared()
locationManager.requestAlwaysAuthorization()

navigationItem.title = "Places"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+ place", style: .plain, target: self, action: #selector(handleSearchPlaces))

tableView.allowsMultipleSelectionDuringEditing = true
}

var placeList = [Place]()
var placesDictionary = [String: Place]()

func fetchPlaces() {
let uid = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference().child("users").child(uid!).child("Places")
ref.observe(.childAdded, with: { (snapshot) in

if let dictionary = snapshot.value as? [String: AnyObject] {

let place = Place()
place.setValuesForKeys(dictionary)

if let addedBy = place.addedBy {
self.placesDictionary[addedBy] = place
self.placeList.insert(place, at: 0)

}

//this will crash because of background thread, so lets call this on dispatch_async main thread
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return placeList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
cell.textLabel?.text = placeList[indexPath.row].place

return cell
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId")
}

最佳答案

你需要这样做

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

guard let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as? Header else { return nil }

// I didn't know what you were using for a count so replace placeList.count with the var you need to use
view.nameLabel.text = "\(placeList.count) of Places"

return view
}

关于swift - 在 UILabel 中显示 tableView 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43968818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com