Swift 在UILabel中显示tableView计数

Swift 在UILabel中显示tableView计数,swift,uitableview,count,header,uilabel,Swift,Uitableview,Count,Header,Uilabel,我想将tableViewCells的数量传递到tableViewHeader UILabel中。例如,标签应根据tableView中的tableViewRows数量(基于Firebase数据)显示“有##行”。提前谢谢 //tableViewHeaderClass class Header: UITableViewHeaderFooterView { var placeList = [Place]() var placesDictionary = [String: Place]

我想将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
}

这很有效,谢谢!