为什么会得到意外的零值传递数据,该数据已由segue加载到表视图中



好吧,我的问题是idk如何通过segue将信息值从uitableviewcell传递到另一个视图控制器,你能帮忙吗,我打开了值,因为数据已经加载了idk该怎么做才能在不崩溃的情况下将信息传递到弹出控制器

这是我设置值的类模型

class MovieCell: UITableViewCell {
//
// MARK: - Class Constants
//
static let identifier = "MovieCell"
let urlImage = "https://image.tmdb.org/t/p/w500"
//
// MARK: - IBOutlets
//
@IBOutlet weak var title: UILabel!
@IBOutlet weak var rating: RatingView!
@IBOutlet weak var releaseDate: UILabel!
@IBOutlet weak var poster: UIImageView!
var titlePopUp: String = ""
func configure(movieDictionary: [String: Any]) {
title.text = (movieDictionary["title"] as! String)
titlePopUp = movieDictionary["title"] as! String

releaseDate.text = (movieDictionary["release_date"] as! String)
do {
let url = URL(string: "(self.urlImage)" + "(movieDictionary["backdrop_path"]!)")
let data = try Data(contentsOf: url!)
self.poster!.image = UIImage(data: data)

}
catch{
print(error)
}
}
}

这是我在第53行中得到错误的视图控制器

class ViewController: UIViewController,UITableViewDataSource, UICollectionViewDataSource, UITableViewDelegate {

var jsonArray: [Any] = []
let movieService = MovieService()
let popUpVC  = popUpViewController()
@IBOutlet weak var moviesTableView: UITableView!
@IBOutlet weak var postersView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
movieService.fetchMovies { jsonArray in
if let jsonArray = jsonArray {
self.jsonArray = jsonArray
self.moviesTableView.reloadData()
self.postersView.reloadData()
}
}
self.moviesTableView.delegate = self
self.moviesTableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.jsonArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MovieCell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! MovieCell
cell.configure(movieDictionary: jsonArray[indexPath.row] as! [String: Any])
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
present(popUpVC, animated: true, completion: nil)
moviesTableView.deselectRow(at: indexPath, animated: true)

self.performSegue(withIdentifier: "popUp", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let destinationVC = segue.destination as! popUpViewController
let data = MovieCell()
destinationVC.movieN = data.title.text!
}
}
}

弹出式控制器就是这个

class popUpViewController: UIViewController {
@IBOutlet weak var poster: UIImageView!
@IBOutlet weak var movieName: UILabel!
@IBOutlet weak var releaseDate: UILabel!
@IBOutlet weak var descriptionMovie: UILabel!

var movieN = String()

override func viewDidLoad() {
super.viewDidLoad()
movieName.text = movieN
// Do any additional setup after loading the view.
}

@IBAction func closePop(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}

im在通过标签和图像时崩溃,冲突错误,意外的零值,但idk为什么数据已经加载在表视图主屏幕中

您没有从单元格中获取数据是因为在prepare方法中,您正在创建单元格的实例。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let destinationVC = segue.destination as! popUpViewController
let data = MovieCell() // This is where the issue is
destinationVC.movieN = data.title.text!
}
}

新创建的单元格实例与已经显示数据的单元格实例没有关系。在新单元格中,所有属性要么为空,要么为nil。这就是为什么会出现"意外零值"错误的原因。

要获取该单元格的值,您应该获取对该单元格的引用

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? MovieCell else { return }
performSegue(withIdentifier: "popUp", sender: cell.titlePopUp)
}

在这里,您可以获得用户点击的单元格,并将其强制转换为MovieCell以访问其titlePopUp属性。

将其传递给sender参数中的prepare方法。

最后,在prepare方法中,将其强制转换回String(因为sender参数的类型为Any(,并将其传递给弹出视图。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let destinationVC = segue.destination as! popUpViewController
destinationVC.movieN = sender as? String
}
}

最后几点注意:这种将JSON作为字典传递的方式可以完成任务,但如果必须更改/维护代码,则会给您带来巨大的麻烦。如果以后你必须将电影的另一个值和标题一起传递到弹出窗口,该怎么办?扩大规模将变得越来越困难。

一定要注意面向对象的编程方式。例如,创建一个名为Movie的类/结构来保存所有数据(标题、发布日期、评级等(。在电影服务中,解析JSON并创建该对象的实例,然后将它们传递给视图控制器。

我已经解决了发件人是:cell not self-

let cell = tableView.cellForRow(at: indexPath) as! MovieCell
self.performSegue(withIdentifier: "popUp", sender: cell)

以及在分段中传递的数据

if segue.identifier == "popUp" {
if let cell = sender as? MovieCell{
let destinationVC = segue.destination as! popUpViewController
destinationVC.movieN = cell.title.text!
let url = cell.urlImage + cell.posterPath
destinationVC.posterUrl = url
destinationVC.descriptionText = cell.descriptionMovie
destinationVC.releaseDateText = cell.releaseDate.text!
}
}

最新更新