【问题标题】:What’s the “cleaner” way to pass data between UIViewControllers在 UIViewController 之间传递数据的“更干净”的方式是什么
【发布时间】:2018-06-06 03:06:00
【问题描述】:

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

故事板:

详情屏幕:

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()

    }
}

视图控制器:

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()
    }
}

【问题讨论】:

  • 你好@rmaddy!非常感谢你的教程。但是,这是在 Objective-C 中吗?
  • 那里有很多答案,很多在 Swift 中。顺便说一句 - 能够阅读 Objective-C 代码以理解所使用的 API 是一项重要的 Swift 技能,因此您可以将其转换为 Swift。
  • @rmaddy 我会读的,再次感谢你!
  • 在您提出其他问题之前,请阅读minimal reproducible example

标签: ios swift xcode uitableview


【解决方案1】:
// 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
    }
}

【讨论】:

  • 您好!非常感谢您对我的帮助,效果很好。
【解决方案2】:

确保您共享的数据将发送到 DestinationViewController 中的 var artistToDisplay: String? 等实际变量,而不是 IBOutlet。

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

【讨论】:

  • 您好!非常感谢你帮助我,伙计。
猜你喜欢
  • 2017-07-16
  • 2014-05-22
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多