Riccardo
Riccardo

Reputation: 299

How do I use prepare segue with tableview cell?

How do I take the text, author... and transfer it to another view controller? I don't understand how to retrieve the data and put it onto a segue and how to call the segue. I have made another view controller and connected the label and its called titleString

var posts = [postStruct]()
var  downloadURL : String = ""


override func viewDidLoad() {
    super.viewDidLoad()


    let ref = Database.database().reference().child("Posts")

    ref.observeSingleEvent(of: .value, with: { snapshot in

        print(snapshot.childrenCount)

        for rest in snapshot.children.allObjects as! [DataSnapshot] {

            guard let value = rest.value as? Dictionary<String,Any> else { continue }

            guard let  title = value["Title"] as? String else { continue }
            guard let  downloadURL = value["Download URL"] as? String else { continue }
            guard let  author = value["Author"] as? String else { continue }
            guard let  date = value["Date"] as? String else { continue }
            guard let  article = value["Article"] as? String else { continue }


            let post = postStruct(title: title, author: author, date: date, article: article, downloadURL: downloadURL)

            self.posts.append(post)


        }

        self.posts = self.posts.reversed(); self.tableView.reloadData()

    })




}


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

    var thePostView = segue.destination as! ArticleViewController
    thePostView.titleString = title!
}




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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].title


    let imageView = cell?.viewWithTag(2) as! UIImageView
    let post = self.posts[indexPath.row];
    imageView.sd_setImage(with: URL(string: post.downloadURL), placeholderImage: UIImage(named: "placeholder"))

 return cell!





}

}

Upvotes: 0

Views: 1458

Answers (2)

Lou Franco
Lou Franco

Reputation: 89152

You need to transfer data inside the prepare for segue method

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

    var thePostView = segue.destination as! ArticleViewController
    thePostView.titleString = title!

    // SET ANY OTHER properties of thePostView here
    // BEWARE THAT OUTLET PROPERTIES ARE NOT HOOKED UP YET
    // SO MAKE OTHER PROPERTIES AND COPY TO OUTLETS IN viewDidLoad

}

Where I put the comment you can set properties of the thePostView. Any @IBOutlet properties in that view controller will be nil at this point, so don't try to set something like a label's text yet. Instead add a regular String property to set and then copy that to a label's text in viewDidLoad()

Upvotes: 1

GetSwifty
GetSwifty

Reputation: 7746

Based on your code it looks like you should be able to use indexPathForSelectedRow:

thePostView.date = posts[tableView.indexPathForSelectedRow.row]

Upvotes: 2

Related Questions