将值从TableViewCell传递到详细信息视图

时间:2016-04-06 04:41:05

标签: ios swift core-data tableview didselectrowatindexpath

就像我筛选过的其他帖子一样,这里是我说我是Swift新手的部分。我有一个应用程序,允许您存储您喜欢的电影的图像和数据(通过核心数据)。这部分工作正常。我可以创建一个新帖子并通过我的tableViewCell查看它。我遇到问题的部分,它在点击一个单元格后在细节页面上显示图像和数据。

我已经阅读了有关此主题的其他帖子和文章,我很确定我需要使用didSelectRowAtIndexPathprepareForSegue,但我只是不明白我应该添加什么以及如何。部分问题是Swift的新功能。请参阅下面的详细信息和我的代码。如果您需要更多详细信息,请告诉我们。任何帮助将不胜感激。如果你知道的话,也会喜欢这个主题的一些有用的阅读材料的链接。除了下面的文件,我还有一个用于创建帖子(PostMovieVC.swift)和一个用于我的自定义单元格(MovieCell.swift)。

ViewController.swift

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!
var movies = [Movie]()

var storeMovieTitle: String!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

override func viewDidAppear(animated: Bool) {
    fetchAndSetResults()
    tableView.reloadData()
}

func fetchAndSetResults() {
    let app = UIApplication.sharedApplication().delegate as! AppDelegate
    let context = app.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "Movie")

    do {
        let results = try context.executeFetchRequest(fetchRequest)
        self.movies = results as! [Movie]
    } catch let err as NSError {
        print(err.debugDescription)
    }
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCellWithIdentifier("MovieCell") as? MovieCell {

        let movie = movies[indexPath.row]
        cell.configureCell(movie)
        return cell
    } else {
        return MovieCell()
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let movie = movies[indexPath.row]

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowMovieDetail" {
        let detailsVC = segue.destinationViewController as! MovieDetailsVC
        let indexPath = tableView.indexPathForSelectedRow!
        let movie = movies[indexPath.row]
        detailsVC.passedTitle = storeMovieTitle

    }
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 169.0
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return movies.count
}

}

MovieDetailsVC.swift

import UIKit
import CoreData

class MovieDetailsVC: UIViewController {

@IBOutlet weak var showMovieImg: UIImageView!
@IBOutlet weak var showTitleLbl: UILabel!
@IBOutlet weak var showSeenWithLbl: UILabel!
@IBOutlet weak var showQuoteLbl: UILabel!
@IBOutlet weak var showLinkLbl: UILabel!

var passedMovieImg: UIImageView!
var passedTitle: String?
var passedSeenWith: String?
var passedQuote: String?
var passedLink: String?

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillAppear(animated: Bool) {

    showMovieImg.image = passedMovieImg
    showTitleLbl.text = passedTitle
    showSeenWithLbl.text = passedSeenWith
    showQuoteLbl.text = passedQuote
    showLinkLbl.text = passedLink

}

@IBAction func closeBtnTapped(sender: AnyObject) {

    dismissViewControllerAnimated(true, completion: nil)

}

}

2 个答案:

答案 0 :(得分:0)

我的问题已通过更新我的代码解决,如下所示:

更新了prepareForSegue func,如下所示:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // get the segue identifier
    if segue.identifier == "ShowMovieDetail" {
        // get a reference for the destination view controller and the selected cell indexPath
        if let MovieDetailsVC = segue.destinationViewController as? MovieDetailsVC, let indexPath = tableView.indexPathForSelectedRow {


            let movie = movies[indexPath.row]
            MovieDetailsVC.movie = movie
        }

    }
}

从MovieDetailsVC中删除了以下代码:

    var passedMovieImg: UIImageView!
    var passedTitle: String?
    var passedSeenWith: String?
    var passedQuote: String?
    var passedLink: String?

将以上内容替换为:

    var movie: Movie?

更新了viewWillAppear,如下所示:

    override func viewWillAppear(animated: Bool) {

       showMovieImg.image = movie?.getMovieImg()
       showTitleLbl.text = movie!.title
       showSeenWithLbl.text = movie!.seenwith
       showQuoteLbl.text = movie!.quote
       showLinkLbl.text = movie!.trailer

    }

答案 1 :(得分:-1)

在movie detailVC中声明一个类型为array或dictionary的变量,在ViewView中的tableview didsselect中初始化该数组,在moviewdetailvc的viewdidload中你可以使用那些细节