简体   繁体   English

使用 Swift 和 Firebase 中的协议从 UITableview 传递数据

[英]Passing data from UITableview using Protocol in Swift and Firebase

I have a tableview called PostsTableView that has a list of user posts with a UIButton called "Comment".我有一个名为 PostsTableView 的 tableview,它有一个用户帖子列表,其中包含一个名为“Comment”的 UIButton。 When a user clicks Comment, I would like it to redirect to CommentViewController that has all the information around the post such as the postText and the user who wrote the post.当用户单击 Comment 时,我希望它重定向到 CommentViewController,其中包含有关帖子的所有信息,例如 postText 和撰写帖子的用户。

Snippets of the code below.下面的代码片段。

PostsTableView帖子表视图

extension PostsTableViewController: PostsTableViewCellDelegate {

  func commentTapped(postInfo: String) {

  //How do I pass postInfo along to CommentViewController

  }

PostsTableViewCell帖子TableViewCell

protocol TableViewCellDelegate {

  func commentTapped(postInfo: String)

}

class PostsTableViewCell: UITableViewCell {

@IBOutlet weak var postTextLabel: UILabel!
@IBOutlet weak var postUserLabel: UILabel!

var postItem: Post!
var delegate: TableViewCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

@IBAction func commentAction(_ sender: Any) {

delegate?.commentTapped(postInfo: postItem.postText)

}
}

Assuming you are using segues, you will call .performSegue(withIdentifier: "mySegueIdentifier", sender: self)假设您正在使用 segues,您将调用.performSegue(withIdentifier: "mySegueIdentifier", sender: self)

You could store the postInfo first by adding a variable to PostsTableViewController您可以通过向postInfo添加一个变量来首先存储PostsTableViewController

var selectedPostInfo: String?

And then setting it in commentTapped(postInfo: String)然后在commentTapped(postInfo: String)

func commentTapped(postInfo: String) {
    selectedPostInfo = postInfo

    performSegue(withIdentifier: "mySegueIdentifier")
}

Now override controller.prepare(for: UIStoryboardSegue, sender: Any?) and set the value on CommentViewController :现在覆盖controller.prepare(for: UIStoryboardSegue, sender: Any?)并在CommentViewController上设置值:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let controller = segue.destination as? CommentViewController {
        controller.postInfo = selectedPostInfo
    }
}

You can do something like this你可以做这样的事情

1) Firstof all refactor the delegate method to take post object as an argument instead of just a String (This helps to provide better User Experience since no need to fetch data already available locally at that moment). 1) 首先重构委托方法以将 post 对象作为参数,而不仅仅是字符串(这有助于提供更好的用户体验,因为此时无需获取本地已经可用的数据)。 Something like this像这样的东西

func commentTapped(postObject: Post) { // I am assuming your Post model has got all the info required by the CommentsViewController
  }

2) Now create a property in CommentsViewController let's say post, which you can I initialise as below code 2)现在在 CommentsViewController 中创建一个属性让我们说 post,你可以初始化如下代码

func commentTapped(postObject: Post) {
 let commentVC = CommentsViewController.initWithNib... // Initialise the Comments ViewController
 commentVC.post = postObject // Assign the post object here
 show(commentVC) // Present here the commentVC as per your requirement of Modal or Push
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM