Varun Varahabotla
Varun Varahabotla

Reputation: 548

How to Segue from Custom TableViewCell to Another ViewController (Swift)

I am attempting to navigate from a TableViewCell to a detail VC indicating details about the current business that is on the cell. Here are the stub tableiewMethods:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("YelpTableBusinessCell", forIndexPath: indexPath) as! YelpTableBusinessCell
    var business:Business = Business(dictionary: businessArray[indexPath.row] as! NSDictionary)
    cell.business = business
    return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    businessToUseTVC = Business(dictionary: businessArray[indexPath.row] as! NSDictionary)
    performSegueWithIdentifier("businessToDetailVC", sender: view);

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let cell = sender as? YelpTableBusinessCell {
        if segue.identifier == "businessToDetailVC" {
            if let ivc = segue.destinationViewController as? AttractionsDetailViewController {
                ivc.businessToUse = self.businessToUseTVC
            }
        }
    }

}

I set two breakpoints, one at the prepareForSegue method and one at the didSelectRowAtIndexPath method

I initialize businessToUseVTC variable in didSelectRowAtIndexPath, but when I run the app, when I am at the AttractionsDetailVC, which contains this bit of code,

func loadYelpInfo() {
        businessName.text = businessToUse.name
        let thumbImageViewData = NSData(contentsOfURL: businessToUse.imageURL!)
        thumbImage.image = UIImage(data: thumbImageViewData!)
        let reviewImageData = NSData(contentsOfURL: businessToUse.ratingImageURL!)
        reviewImage.image = UIImage(data: reviewImageData!)
        categories.text = businessToUse.categories
        reviews.text = "\(businessToUse.reviewCount!) Reviews"
        distanceLabel.text = businessToUse.distance
    }

the code breaks, saying that businessToUse, set inside DetailsVC, is nil.

Any help would be greatly appreciated.

Upvotes: 2

Views: 216

Answers (1)

kebabTiger
kebabTiger

Reputation: 652

First, make sure you ctrl drag from the VIEWCONTROLLER, not the individual cell.

Second, the sender is not of type YelpTablseBusinessCell, i believe this is your problem.

Upvotes: 2

Related Questions