简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM