gpt4 book ai didi

ios - 如何获取 TableView 中可见行内容背后的键?

转载 作者:行者123 更新时间:2023-11-29 05:41:50 25 4
gpt4 key购买 nike

我想做的是:

我的tableview从firebase数据库中获取帖子,当其他用户看到该帖子时,我想获取他们当前正在查看的帖子的 key 。我想将此 key 存储为可以在其他地方使用的字符串。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "post")
let imageView = cell?.viewWithTag(1) as! UIImageView
imageView.sd_setImage(with: URL(string: posts[indexPath.row].postUrl), placeholderImage: UIImage(imageLiteralResourceName: "SymboleBLANC3x-1.jpg"))
return cell!
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.size.height
}

override func viewDidLoad() {
super.viewDidLoad()
loadPosts()
}

var posts = [Post]()
func loadPosts() {
Database.database().reference().child("posts").observe(.childAdded) { (snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any]{
let url = dict["postUrl"] as? String
let rank = dict["rank"] as? Int
let post = Post(urlString: url!, rankNumber: rank ?? 0)
self.posts.append(post)
self.tableView.reloadData()

}
}

最佳答案

您可以从下面的代码中获取最后一个可见索引,并在此基础上从主数组中获取内容。

let visibleArray = tableView.indexPathsForVisibleRows
let indexPath = visibleArray?.value(forKeyPath: "@max.self") as? IndexPath

关于ios - 如何获取 TableView 中可见行内容背后的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56478648/

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