【发布时间】:2017-02-17 23:43:49
【问题描述】:
我在我的 tableview 单元格中创建了类似的系统。但是它有问题。
- 如果我喜欢一件事,每 7 个单元格也越来越喜欢,为什么? reusableCell 有什么用吗?
- 最好的方法是什么,我做错了吗?
这是like按钮系统:
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
}
}
这是我的like系统函数:
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.. 不知道如何继续。请带我回到赛道。
这是我的结构:
【问题讨论】:
-
这与 [您的上一个问题](stackoverflow.com/q/39937930) 中的代码相同。使用完全相同的代码打开多个问题通常表明您应该创建MCVE 来隔离问题。
-
哥们,你的问题有什么消息吗?
-
我实际上被 indexPath.rows 困住了,因为我不得不在 tableview 函数之外使用那些,所以我放弃了。但我计划今天阅读您的问题并接受它:)
-
ooooookay,结果如何? :P
标签: ios swift firebase tableview firebase-realtime-database