gpt4 book ai didi

ios - “cleaner”在UIViewControllers之间传递数据的方式是什么

转载 作者:可可西里 更新时间:2023-11-01 01:08:48 24 4
gpt4 key购买 nike

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

Storyboard:

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

}
}

View Controller :

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

最佳答案

// 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
}
}

关于ios - “cleaner”在UIViewControllers之间传递数据的方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50711685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com