Charles Jr
Charles Jr

Reputation: 9129

Fatal Error: Found Nil While Unwrapping Optional Value TableViewCell to UIViewController

I don't know what's happening. Need help with either segue, didSelectRowAtIndexPath, or casting. I have a tableview on top of a uiviewcontroller with a custom tableviewcell. I successfully got an 4 arrays of Strings to present themselves in the custom cell. Now I'm trying to pass this information to another uiviewcontroller. I'm creating a public Int as....

var selectedVideoIndex: Int!

didSelectRowAtIndexPath is.....

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {

        let cell = self.resultsTableView.dequeueReusableCellWithIdentifier("searchCell", forIndexPath: indexPath) as! ResultsTableViewCell

        let newIndexPath = resultsTableView.indexPathForCell(cell)!
        selectedVideoIndex = newIndexPath.row
        self.performSegueWithIdentifier("playerSegue", sender: self)


    }

and prepareForSegue is.....

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier == "playerSegue"
        {
            let controller = segue.destinationViewController as! VideoViewController

            controller.vidTitleLBL.text = self.videoTitle[selectedVideoIndex]
            controller.videoId = self.videoId[selectedVideoIndex]

        }
    }

Also I'm manually creating segue in storyboard. Should I do it from cell to the new controller or from the view? Thanks.

****New****

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{

    selectedVideoIndex = indexPath.row
    self.performSegueWithIdentifier("seguePlayer", sender: self)

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("searchCell", forIndexPath: indexPath) as! ResultsTableViewCell
    cell.titleLabel.text = self.videoTitle[indexPath.row]
    cell.titleLabel.adjustsFontSizeToFitWidth = true
    cell.descriptionLabel.text = self.videoDescription[indexPath.row]
    cell.descriptionLabel.adjustsFontSizeToFitWidth = true
    let url = NSURL(string: self.videoIMG[indexPath.row])
    let data = NSData(contentsOfURL: url!)
    cell.videoImage.image = UIImage(data: data!)

    return cell

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier == "seguePlayer"
        {

            print(self.videoTitle[selectedVideoIndex])
            print(self.videoId[selectedVideoIndex])
            let controller = segue.destinationViewController as! VideoViewController
            controller.vidTitleLBL.text = self.videoTitle[selectedVideoIndex]
            controller.videoId = self.videoId[selectedVideoIndex]

        }
    }

Got it to successfully print. Now giving error casting at..

controller.vidTitleLBL.text = self.videoTitle[selectedVideoIndex]
            controller.videoId = self.videoId[selectedVideoIndex]

What do think this is?? Thank for the printing suggestion btw.

Upvotes: 0

Views: 65

Answers (1)

meda
meda

Reputation: 45490

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) already passes the indexPath so you should not create a cell in that delegate:

selectedVideoIndex = indexPath.row

should be enough, remove the cell code .

Or better way in prepareForSegue use:

let indexPath = tableView.indexPathForSelectedRows()

then use that indexPath

print(self.videoTitle[indexPath.row])
print(self.videoId[indexPath.row])

Upvotes: 1

Related Questions