如何从自定义TableViewCell到另一个ViewController(Swift)

时间:2015-08-29 22:47:03

标签: ios swift uitableview

我正在尝试从TableViewCell导航到详细VC,指示有关单元格上当前业务的详细信息。这是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
            }
        }
    }

}

我设置了两个断点,一个位于prepareForSegue方法,一个位于didSelectRowAtIndexPath方法

我在businessToUseVTC中初始化didSelectRowAtIndexPath变量,但是当我运行应用程序时,当我在使用包含这段代码的AttractionsDetailVC时,

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
    }

代码断开,说明在DetailsVC中设置的businessToUse是nil。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

首先,确保从VIEWCONTROLLER中拖动,而不是单个单元格。

其次,发件人不属于YelpTablseBusinessCell类型,我相信这是您的问题。