2

I gotta populate a UIViewController using data from a UITableView. So, when the user click on each UITableview Cell, another screen should appear filled with some data from the respective clicked UITableView Cell. I don't have certain if I should do it using "Segue" to the other screen, or if there's any better and "clean" way to do that. What would you guys recommend me to do?

Storyboard:

enter image description here

Details Screen:

import UIKit

class TelaDetalheProdutos: UIViewController {
    @IBOutlet weak var ImageView: UIImageView!
    @IBOutlet weak var labelNomeEDesc: UILabel!
    @IBOutlet weak var labelDe: UILabel!
    @IBOutlet weak var labelPor: UILabel!
    @IBOutlet weak var labelNomeProduto: UILabel!
    @IBOutlet weak var labelDescricao: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

ViewController:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UITableViewDataSource {

    @IBOutlet weak var tableViewTopSell: UITableView!
    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewBanner: UICollectionView!


    var dataSource: [Content] = [Content]()
    var dataBanner: [Banner] = [Banner]()
    var dataTopSold: [Top10] = [Top10]()

    override func viewDidLoad() {
        super.viewDidLoad()
        //SetupNavBarCustom
        self.navigationController?.navigationBar.CustomNavigationBar()
        let logo = UIImage(named: "tag.png")
        let imageView = UIImageView(image:logo)
        self.navigationItem.titleView = imageView
        //CallAPIData
        getTopSold { (data) in
            DispatchQueue.main.async {
                self.dataTopSold = data
                self.tableViewTopSell.reloadData()
            }
        }
        getBanner { (data) in
            DispatchQueue.main.async {
            self.dataBanner = data
            self.collectionViewBanner.reloadData()
            }
        }
        getAudiobooksAPI { (data) in
            DispatchQueue.main.async {
                self.dataSource = data
                self.collectionView.reloadData()
            }
        }
    }
    //CollectionView
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (collectionView == self.collectionView) {
            return  self.dataSource.count
        }else{
            return self.dataBanner.count
        }}
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if (collectionView == self.collectionView) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell

        let content = self.dataSource[indexPath.item]

        cell.bookLabel.text = content.descricao
        cell.bookImage.setImage(url: content.urlImagem, placeholder: "")

        return cell

        }else if (collectionView == self.collectionViewBanner) {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCellBanner", for: indexPath) as! CollectionViewCell

            let content = self.dataBanner[indexPath.item]

            cell.bannerImage.setImage(url: content.urlImagem, placeholder: "")


            return cell
        }
        return UICollectionViewCell()
    }
//TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataTopSold.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "topSoldCell", for: indexPath) as! TableViewCell

    let content = self.dataTopSold[indexPath.item]
    cell.labelNomeTopSell.text = content.nome
    cell.imageViewTopSell.setImage(url: content.urlImagem, placeholder: "")
    cell.labelPrecoDe.text = "R$ \(content.precoDe)"
    cell.labelPrecoPor.text = "R$ 119.99"
    return cell
}
}

extension UIImageView{
    func setImage(url : String, placeholder: String, callback : (() -> Void)? = nil){
        self.image = UIImage(named: "no-photo")

        URLSession.shared.dataTask(with: NSURL(string: url)! as URL, completionHandler: { (data, response, error) -> Void in

            guard error == nil else{
                return
            }
            DispatchQueue.main.async(execute: { () -> Void in
                let image = UIImage(data: data!)
                self.image = image

                if let callback = callback{
                    callback()
                }


            })

        }).resume()
    }
}

2 Answers2

2
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    switch  segue.destination{
    case is DestinationViewController:
        let vc = segue.destination as! DestinationViewController
        //Share your data to DestinationViewController
        //Like vc.variableName = value

    default:
        break
    }
}
Vivek Kumar
  • 405
  • 5
  • 15
0

Make sure that the data your sharing is going to an actual variable like var artistToDisplay: String? in the DestinationViewController, and not an IBOutlet.

You may also need to implement the tableView(_:didSelectRowAt:_) and performSegue(withIdentifier:sender:) methods to begin the segue.