如果每个用户旁边都有一个喜欢按钮,为什么滚动触发随机用户的喜欢按钮会突出显示?



我使用 self.like.alpha = 0.5 将被点赞的用户旁边的"喜欢"按钮变灰。滚动会导致突出显示有时会消失并显示在其他用户旁边。

我在代码中使用了self.like.alpha = 0.5,但它没有任何变化。

@IBAction func likePressed(_ sender: Any) {
self.like.alpha = 0.5

let ref = Database.database().reference()
let keyToPost = ref.child("likes").childByAutoId().key


ref.child("humans").child(self.postID).observeSingleEvent(of: .value, with:  {(snapshot) in
if let humans = snapshot.value as? [String: AnyObject] {
let updateLikes: [String: Any] = ["humansWhoLike/(keyToPost)" : Auth.auth().currentUser!.uid]
ref.child("humans").child(self.postID).updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in

if error == nil {
ref.child("humans").child(self.postID).observeSingleEvent(of: .value, with: { (snap) in
if let properties = snap.value as?[String: AnyObject]{
if let likes = properties["humansWhoLike"] as? [String : AnyObject] {
let count = likes.count
let update = ["likes" : count]
ref.child("humans").child(self.postID).updateChildValues(update)


}
}
})
}
})
}
})

ref.removeAllObservers()

}

我需要的是单击以灰显的"喜欢"按钮。它必须保持灰色,并且灰色不应跳转到其他用户的喜欢按钮。

/第一个答案后更新了代码

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
let like = cell.viewWithTag(3) as! UIButton

let immy = cell.viewWithTag(1) as! UIImageView
let person: Userx = humans[indexPath.row]


cell.lblName.text = person.Education
cell.postID = self.humans[indexPath.row].postID

if let PhotoPosts = person.PhotoPosts {
let url = URL(string: PhotoPosts)
immy.sd_setImage(with: url)

}


return cell

}

请记住,tableView 单元格是可重用的。当您取消排队时,您无法对现有值进行任何假设。如果您将某个单元格标记为喜欢(使用按钮格式(,则当重复使用该单元格时,格式仍然存在。

在 cellForRowAt 函数中取消单元格排队时,需要根据数据存储重置所有值。

我在理解您的数据库设计/用法时遇到了一些麻烦,但根据您添加到帖子中的代码:

let currUser = Auth.auth().currentUser!.uid // better to add this as a VC level variable as you will do this lookup a lot.
let likeArray = person.humansWhoLike ?? [] 
let likeStatus = likeArray.contains(currentUser) 
//from your code, 'like' is the button to be formatted
like.alpha = likeStatus ? 0.5 : 1.0

最新更新