1

我是新的 iOS 开发人员,仍在学习基础知识,我的查询是当我将数据从我的主 tableView 控制器传递到其他视图控制器时,所以它顺利通过,但输出结果在控制台中显示为 nil,如下所示 ,你可以看到控制台中的输出为零。这是我的第二个视图控制器,名为 movieDetailViewController

下面是我的主 tableView 控制器,我从中调用我的第二个视图控制器。

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? movieDetailViewController
    {
        destination.film = movies[(tables.indexPathForSelectedRow?.row)!] as? Movie
    }
  }
}

这里的结构 Movie 是一个普通的 swift 文件,其中包含我要打印的属性,如下所示:

import Foundation

struct Movie {

     var title: String
     var overview: String
     var poster_path: String

}

现在,正如我描述的那样,当传递给另一个视图控制器时输出显示为零。我从 1 周起就陷入了这个问题,也进行了很多搜索,但没有解决方案对我有帮助。

4

3 回答 3

0

试试这个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetails", let destination = segue.destination as? movieDetailViewController {
        if let cell = sender as? UITableViewCell, let indexPath = tables.indexPath(for: cell) {
            destination.film = movies[indexPath.row] as? Movie
        }
    }
}
于 2017-12-06T11:11:17.900 回答
0

试试这个

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


 let mv = movies[indexPath.row]

 performSegue(withIdentifier: "showDetails", sender: mv)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let destination = segue.destination as? movieDetailViewController
{
    destination.film = sender as? Movie
}
}
于 2017-12-06T11:14:56.663 回答
0

在movieTable View Controller 类中替换此代码

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? movieDetailViewController
    {
        if let selectedIndex = tables.indexPathForSelectedRow?.row,
            let selectedTitle = movies[selectedIndex]["title"],
            let selectedOverView = movies[selectedIndex]["overview"],
            let selectedPosterPath : String = movies[selectedIndex]["poster_path"] as! String
            {
                destination.film = Movie.init(title: selectedTitle as! String, overView: selectedOverView as! String, posterPath:selectedPosterPath) as! Movie
            }
    }
}

将初始化程序添加到您的结构

init(title t: String, overView: String, posterPath pp: String) {
    title = t
    overview = overView
    poster_path = pp
}

将您的viewDidLoad代码替换movieDetailsViewController

override func viewDidLoad() {
        super.viewDidLoad()
        if let title = film?.title,
           let movieoverview = film?.overview,
            let urlString = film?.poster_path
        {
            movieTitle.text = title
            print(movieTitle.text)

            movieOverview.text = movieoverview

            print(movieOverview.text)

            let urlString = URL(string: ("http://image.tmdb.org/t/p/w185/") + urlString)
            moviePoster.downloadedFrom(url: urlString!)

        }


    }

希望这可以帮助。快乐编码。

于 2017-12-08T23:32:09.603 回答