我将数据从一个视图控制器传递到另一个视图控制器。第一个视图控制器有一个collectionviewcell,我正在调整的控制器有一个tableviewcell。当我点击同时点击用户的集合视图的单元格时,我想要进入一个详细的视图控制器,其中包含用户制作的所有帖子的列表(存储在firebase中)
这是我到目前为止: - 这是我的控制器与collectionviewcell
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)
}
}}
现在当我点击单元格时,我会进入详细的视图控制器,它会显示我数据库中所有用户发布的所有帖子,我知道这是因为在我的segue中我写了vc.posts = posts
。我是swift的新手,我不知道如何设置它以便当我单击一个单元格时(也就是当我点击一个用户时),然后一个详细的tableviewcontroller显示我的所有帖子,只有该用户已经< / p>
同样,我正在尝试仅显示由个人用户上传的帖子
答案 0 :(得分:1)
UsersProfileViewController
。UsersProfileViewController
包含一个属性posts
,代表用户发布的帖子。UsersProfileViewController
,使其仅显示所选用户发布的帖子?cellForItemAtIndexPath
中设置按钮indexPath cellForItemAtIndexPath
中设置按钮的目标selectedUserName
(查看控制器属性)。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)")
}
}
struct
的{{1}}(而不是使用字典,编码时容易出错)User
的{{1}},Post会有一个名为User的属性来映射用户 - https://itunes.apple.com/us/book/the-swift-programming-language-swift-3-1/id881256329?mt=11 struct
,每种语言都有一些您可以利用的功能。 Post
)答案 1 :(得分:0)
你可以在storyboard中将segue从collectionViewController设置为tableViewController,然后给它一个标识符(非常重要)。
在collectionViewController中,覆盖prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc = segue.destinationViewController as? YourTableViewControllerClassName {
// assign data you want to pass here
}
}
然后在collectionView:didSelectItemAt
方法中,请致电performSegueWithIdentifier("yourID", sender: self)
希望这有帮助!