gpt4 book ai didi

ios - 如何实现prepareForReuse()来防止褪色的单元格表现得很奇怪?

转载 作者:行者123 更新时间:2023-11-29 05:30:33 27 4
gpt4 key购买 nike

我使用 self.like.alpha = 0.5 来突出显示 .被喜欢的用户旁边的“喜欢”按钮。滚动会导致突出显示有时消失并出现在其他用户旁边。

我发现我可以使用 A)prepareForReuse 或 B)执行类似伪代码的操作(尽管 IsLiked 必须以某种方式限制在该 session 中):

cell.like.alpha = item.isLiked ? 0.5 : 1

这是我的代码:

在 View ControllerTableViewCell.swift中:

 @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()
}

在 homepage.swift 中(使用 like 按钮的地方):

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
}

当单元格在滚动后被重用时,我需要像淡入淡出这样的功能,以便不会消失,也不会转到其他用户那里。

数据库结构:

 "humans" : {
"5VbL3Adj7teM2KNJkF4GIexBJhE2" : {
"Coordinates" : {
"latitude" : 42.8864,
"longitude" : 78.8784
},
"Education" : "Yale Accounting",
"PhotoPosts" : "https://firebasestorage.googleapis.com/v0/b/daylike-2f938.appspot.com/o/images%2FPhotoPosts?alt=media&token=42d76567-ac42-4728-9914-1d7c2fa4d5e6",
"WhatIamConsideringBuying" : "Twitter: Carla9",
"caption" : 1565896677651,
"caption1" : {
"caption1" : 1566734644170,
"keyToPost" : "-Ln7eM9S_pSQ_aqRPz0k"
},
"likes" : 82,
"peopleWhoLike" : {
"-LmLjHwwGj1kt5qLM20X" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLtlp5Sm900SV8xP4i" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLuOzQ0TZ9uJ_uNkkg" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLultcrrsG2NjEYoTe" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLvMseSmFhnxEGGctU" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LmLvVQokDCZLFrnhaLu" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",

},
"postID" : "5VbL3Adj7teM2KNJkF4GIexBJhE2",
"users" : "carla19martin@me.com"
},
"9RYUttVhCzXKs6H1XnZ63TZ8Dun2" : {

最佳答案

您需要在cellForRow方法中设置每个单元格的alpha。您不应根据单元格本身向您的 likePressed() 方法发送操作来更改它。相反,它应该反射(reflect)您的底层数据模型,我假设您已在本地缓存。

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

// You need to set the alpha here in cellForRow method
// Your idea using nil coalescing is good, as it must always be set afresh for the relevant cell (so it is correct when reused)
// Get value of isLiked - this depends on your database and local cache
// This is an example - accessing the isLiked property may be different depending on your data model

cell.like.alpha = posts[indexPath.row].isLiked ? 0.5 : 1

return cell
}

关于ios - 如何实现prepareForReuse()来防止褪色的单元格表现得很奇怪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57642086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com