2017-05-14 8 views
-1

テーブルビューの行カウント値を表示するtableViewHeaderにUILabelがあります。行が削除された後、行が削除されるとすぐに、tableViewHeader UILabelのカウント値を更新したいと考えています(ユーザーが別のVCに行き、カウント値が更新されたtableViewに戻った後ではありません)。再度、セルがtableViewから削除されると、私はUILabelの値を同じビューで更新したいと思います。助けてくれてありがとう!更新セルが削除された後のtableViewHeaderのUILabel

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

    } 
} 

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

テーブル「tableView.reloadData()を再ロードしようとしましたか? –

+0

デリゲート関数didEndEditingRowAtを試しましたか? –

答えて

1

を削除し、これを試してみてください:

placeList.remove(at: indexPath.row) 
tableView.beginUpdates() 
tableView.deleteRows(at: [indexPath], with: .left) 
tableView.reloadSections(IndexSet(integer: indexPath.section), with: .automatic) 
tableView.endUpdates() 
+0

、ありがとう! – user3708224

+0

恐ろしい:)乾杯 –

関連する問題