当我尝试将数据从我的UITableView传递到另一个UIViewController时,我遇到了一个问题。我应该怎么做我的“If”语句才能传递每个选定单元格的数据?你能帮我个忙吗?
的ViewController:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate {
@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()
//Delegate TableView
self.tableViewTopSell.delegate = self
//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
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segueId", sender:self.dataTopSold[indexPath.item])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueId" {
let des = segue.destination as! TelaDetalheProdutos
des.stringNomeeDesc = sender as? String
}
}
}
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()
}
}
AudioBook文件:
import Foundation
//Categorias
struct Contents : Decodable {
let data : [Content]
}
struct Content : Decodable {
let id : Int
let descricao : String
let urlImagem : String
}
//Banner
struct BannerData : Decodable {
let data : [Banner]
}
struct Banner : Decodable {
let id : Int
let urlImagem : String
let linkUrl : String
}
//Top10
struct Top10Data:Decodable {
let data: [Top10]
}
struct Top10:Decodable {
let id : Int
let nome : String
let urlImagem : String
let descricao : String
let precoDe : Int
}
详情ViewController:
import UIKit
class TelaDetalheProdutos: UIViewController {
//Properties
@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!
//Strings
var stringImageView = String()
var stringNomeeDesc = String()
var stringLabelDe = String()
var stringLabelPor = String()
var stringNomeProduto = String()
var stringlabeDescricao = String()
override func viewDidLoad() {
super.viewDidLoad()
***//What should I do here?***
}
}
[编辑1]:
我这样做,但是我遇到了这个崩溃:
无法将“Top10”类型的值指定为“String”
类型
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
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segueId", sender:self.dataTopSold[indexPath.row])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueId" {
let des = segue.destination as! TelaDetalheProdutos
des.stringNomeeDesc = sender as! Top10
}
}
[编辑3] -SOLVED - 在Storyboard中发生的事情我将UITableViewCell链接到其他屏幕而不是链接ViewController 。这就是它出现两次的原因。
当屏幕在选择单元格后进行转换时,包含结果的下一个屏幕会自动出现两次。它第一次有空字段,在1秒或更短的时间后,又有一次自动转换,但这次包含了我传递的所有数据。当我点击“返回”TopBar按钮时会发生同样的情况。可能会发生什么?
答案 0 :(得分:2)
你可以尝试
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segueId", sender:self.dataTopSold[indexPath.row])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueId" {
let des = segue.destination as! TelaDetalheProdutos
des.item = sender as! Top10
}
}
//
class TelaDetalheProdutos: UIViewController {
var item:Top10?
}
答案 1 :(得分:0)
将选定的索引路径保存在did select
中准备segue获取数据并将其传递给DetailViewController
//in prepare for segue
let content = self.dataTopSold[indexPath.item]
//cast segue.destination controller to DetailViewController and pass content
guard let detailViewController = segue.destinationViewController as? DetailViewController else {return}
detailViewController.content = content