テーブルビューのセルに似たシステムを作成しました。しかしそれには問題があります。テーブルビューセル内のシステムのようなFirebase
- 私は1つのことが好きなら、7番目のセルはすべて同じようになっています。なぜですか?再利用可能なセルがありますか?
- これを実行する最善の方法は何ですか、私はそれを完全に間違っていますか?
これは、のようなボタンシステムである:
cell.likeButton.addTarget(self, action: #selector(self.tapped), for: .touchUpInside)
func tapped(sender: DOFavoriteButton) {
if sender.isSelected {
// deselect
sender.deselect()//+1 like
} else {
// select with animation
sender.select()//-1 like
}
}
そして、これは私のlikeSystem機能である:
func likeSystem(sender: DOFavoriteButton, cellForRowAt indexPath: IndexPath){
if sender.isSelected {
let cell = tableView.dequeueReusableCell(withIdentifier: "snusProductsCell", for: indexPath) as! SnusProductTableViewCell
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
(currentData:FIRMutableData!) -> FIRTransactionResult in
if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
var stars : Dictionary<String, Bool>
stars = post["hasLiked"] as? [String : Bool] ?? [:]
var starCount = post["likes"] as? Int ?? 0
if let _ = stars[uid] {
// Unstar the post and remove self from stars
starCount -= 1
stars.removeValue(forKey: uid)
} else {
// Star the post and add self to stars
starCount += 1
stars[uid] = true
sender.deselect()
}
post["hasLiked"] = starCount as AnyObject?
post["likes"] = stars as AnyObject?
// Set value and report transaction success
currentData.value = post
return FIRTransactionResult.success(withValue: currentData)
}
return FIRTransactionResult.success(withValue: currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
}else{
sender.select()
}
}
私の脳は...継続する方法がわからないATMをクラッシュされます。私をトラックに連れて戻ってください。
これは私の構造である:
http://stackoverflow.com/a/39492396/6297658 – Dravidian
これは[あなたの前の質問](http://stackoverflow.com/q/39937930)と同じコードです。まったく同じコードで複数の質問を開いていることは、通常、問題を特定するために[MCVE](http://stackoverflow.com/help/mcve)を作成する必要があるという兆候です。 –
質問にはどんなニュースがありますか? –