什么是在UIViewControllers之间传递数据的“更清洁”方式

时间:2018-06-06 03:06:01

标签: ios swift xcode uitableview

我必须使用UITableView中的数据填充UIViewController。因此,当用户单击每个UITableview Cell时,应显示另一个屏幕,其中填充了来自相应单击的UITableView Cell的一些数据。我不确定是否应该使用“Segue”到另一个屏幕,或者是否有更好和“干净”的方式来做到这一点。你们会建议我做什么?

故事板:

enter image description here

详细信息屏幕:

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 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

确保您的共享数据在DestinationViewController中变为实际变量,如var artistToDisplay: String?,而不是IBOutlet。

您可能还需要实施tableView(_:didSelectRowAt:_)performSegue(withIdentifier:sender:)方法来开始segue。