2017-05-14 5 views
0

tableViewCells의 수를 tableViewHeader UILabel에 전달하고 싶습니다. 예를 들어, 레이블에는 "There are ## Rows"라고 표기되어야합니다 (Base 테이블에있는 tableViewRows 수 (Firebase 데이터 기준). 미리 감사드립니다!display table UILabel의 뷰 카운트

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

답변

0

당신이

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 
} 
+0

, 감사를 할 필요가! – user3708224