gpt4 book ai didi

swift - 转到详细的 TableView Controller

转载 作者:行者123 更新时间:2023-11-28 15:38:43 25 4
gpt4 key购买 nike

我正在将数据从一个 View Controller 传递到另一个。第一个 View Controller 有一个 collectionviewcell,而我要连接的 Controller 有一个 tableviewcell。当我单击同时单击用户的 Collection View 的单元格时,我想进入一个详细的 View Controller ,其中包含用户发布的所有帖子的列表(存储在 firebase 中)

这是我目前所拥有的:-这是我的带有 collectionviewcell 的 Controller

class WelcomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{

var posts = NSMutableArray()
var databaseRef = FIRDatabase.database().reference()
var loggedInUser = FIRAuth.auth()?.currentUser

override func viewDidLoad() {
super.viewDidLoad()

loadData()

}

func loadData(){
if (FIRAuth.auth()?.currentUser) != nil{
FIRDatabase.database().reference().child("books").observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in


let loggedInUserData = snapshot
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
self.posts.add(post.value)
}
self.CollectionView.reloadData()


}})}
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {


if segue.identifier == "UsersProfile" {

if let indexPath = sender as? IndexPath{
let vc = segue.destination as! UsersProfileViewController
let post = self.posts[indexPath.row] as! [String: AnyObject]
let posted = self.posts[indexPath.row] as! [NSMutableArray: AnyObject]
let username = post["username"] as? String
let userpicuid = post["uid"] as? String
let userpostuid = post["uid"] as? NSMutableArray
vc.username = username
vc.userpicuid = userpicuid
vc.posts = posts
print(indexPath.row)
}
}}

现在,当我单击单元格时,我会进入详细 View Controller ,它会显示我数据库中所有用户发布的所有帖子,我知道这是因为在我的 segue 中我写了 vc.posts =帖子。我是 swift 的新手,我不知道如何设置它,以便当我单击一个单元格时(这也是我单击用户时)然后一个详细的 tableviewcontroller 向我显示所有帖子,只有那个用户制作了

同样,我试图只显示个人用户上传的帖子

最佳答案

概述:

  • Collection View 显示帖子列表。
  • 每个单元格代表一个帖子。
  • 每个单元格都有一个带有用户图像的按钮,当点击它时,应用程序会进入 UsersProfileViewController
  • UsersProfileViewController 包含一个属性 posts,代表用户发布的帖子。

问题:

  • 如何设置 UsersProfileViewController 使其只显示所选用户发布的帖子?

方法:

  • 每个单元格都出列并重新使用。
  • 点击按钮时,它需要知道按钮对应的索引路径。
  • 一旦包含按钮的单元格的 indexPath 已知,我们就可以确定与其关联的用户。
  • 已知用户后,根据所选用户过滤帖子。

步骤:

  1. 使用 indexPath 作为属性创建按钮的子类
  2. 在您的单元格中使用该自定义按钮
  3. cellForItemAtIndexPath中设置按钮indexPath
  4. cellForItemAtIndexPath 中设置按钮的目标
  5. 按下按钮时,确定相应的用户名并将其存储为selectedUserName( View Controller 属性)。
  6. prepareForSegue 中根据 selectedUserName 过滤帖子

示例代码:

class CellButton : UIButton {

var indexPath : IndexPath?
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CustomCollectionViewCell

let post = posts[indexPath.row] as? [String : Any]

cell.backgroundColor = .red
cell.titleLabel.text = post?["title"] as? String
cell.button.setTitle(post?["username"] as? String, for: .normal)
cell.button.addTarget(self, action: #selector(tappedOnUserButton(button:)),
for: .touchUpInside)
cell.button.indexPath = indexPath

return cell
}

@objc private func tappedOnUserButton(button: CellButton) {

guard let indexPath = button.indexPath else {
return
}

print("button pressed - \(indexPath)")

let post = posts[indexPath.row] as? [String : Any]

selectedUserName = post?["username"] as? String
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "UsersProfile" {


//Filter posts based on username
//Assumption: posts contains all the posts.
let postsForUser = posts.filter {

guard let post = $0 as? [String : Any] else {
return false
}

return post["username"] as? String == selectedUserName
}

print("postForUser = \(postsForUser)")

}
}

首选方法:

  • 创建一个名为 Userstruct(而不是使用在编码时容易出错的字典)
  • 创建一个名为 Poststruct,Post 将有一个名为 User 的属性来映射用户 - https://itunes.apple.com/us/book/the-swift-programming-language-swift-3-1/id881256329?mt=11
  • 请学习Swift,每种语言都有一些您可以利用的特性。
  • 不要强制展开变量,除非你 100% 确定它会包含一个非 nil 值
  • 尽可能使用原生类型(例如使用 Swift Array 而不是 NSMutableArray)
  • 请清楚地解释您的问题,这样其他人就不必花时间努力去理解。 (准确清晰并隔离问题,并使用 markdown 进行格式化)

关于swift - 转到详细的 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43988827/

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