Closed. This question needs details or clarity。它当前不接受答案。
想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
2年前关闭。
我想将tableViewCells的数量传递到tableViewHeader UILabel中。例如,标签应根据tableView中有多少tableViewRows(基于Firebase数据)说“有##行”。提前致谢!
// tableViewHeaderClass
// tableViewController类
想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
2年前关闭。
我想将tableViewCells的数量传递到tableViewHeader UILabel中。例如,标签应根据tableView中有多少tableViewRows(基于Firebase数据)说“有##行”。提前致谢!
// tableViewHeaderClass
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/
10-13 04:19