如何将解析数据从tableview传递给详细视图控制器?

时间:2015-07-15 02:36:41

标签: ios swift uitableview parse-platform detailview

新解析后端并一起编码......

希望通过UIStoryboard segue将数据从我的HomeTimelineViewController(VC#1)传递到我的ProductDetailViewController(VC#2)。

这是我的VC#1代码:

import UIKit
import Parse

class HomeTimelineViewController: UIViewController, UITableViewDelegate {


    @IBOutlet var homeTimelineTableView: UITableView!


    var imagePNG = [PFFile]()
    var shortDescription = [String]()
    var productTitle = [String]()
    var productPrice = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var query = PFQuery(className: "Product")
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock {
            (products: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // success fetching objects
                for product in products! {

                    self.imagePNG.append(product["imagePNG"] as! PFFile)
                    self.shortDescription.append(product["shortDescription"] as! String)
                    self.productTitle.append(product["title"] as! String)
                    self.productPrice.append(product["price"] as! String)

                }

                // reload the timeline table
                self.homeTimelineTableView.reloadData()

            }else {

                println(error)
            }
        }

    }

    // table view population beginning
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return imagePNG.count

    }

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


        let singleCell: ProductTableViewCell = tableView.dequeueReusableCellWithIdentifier("Product Cell") as! ProductTableViewCell

        // short description
        singleCell.productCellShortDescriptionLabel.text = shortDescription[indexPath.row]
        // price
        singleCell.productCellPriceLabel.text = productPrice[indexPath.row]
        // title
        singleCell.productCellTitleLabel.text = productTitle[indexPath.row]
        // image
        imagePNG[indexPath.row].getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in

            if imageData != nil {

                let image = UIImage(data: imageData!)
                singleCell.productCellImageview.image = image
            }
        }
        return singleCell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var productDetailVC: ProductDetailViewController = segue.destinationViewController as! ProductDetailViewController

        productDetailVC.productDetailTitleLabel = shortDescription
    }   
}

这是我的VC#2代码(DetailView):

import UIKit
import Parse

class ProductDetailViewController: UIViewController {

    @IBOutlet var tagProduct: UIButton!
    @IBOutlet var productDetailTitle: UITextView!
    @IBOutlet var productDetailImage: UIImageView!
    @IBOutlet var productDetailShortDescription: UITextView!
    @IBOutlet var productDetailLongDescription: UITextView!

    var productDetailTitleLabel = [String]()
    var productDetailImageView = [PFFile]()
    var productDetailShortDescriptionLabel = [String]()
    var productDetailLongDescriptionLabel = [String]()



    override func viewDidLoad() {
        super.viewDidLoad()


        // tag product button
        tagProduct.layer.borderColor = UIColor.blackColor().CGColor
        tagProduct.layer.borderWidth = 0.5
        tagProduct.layer.cornerRadius = 5





        productDetailTitle.text = productDetailTitleLabel
        productDetailShortDescription.text = productDetailShortDescriptionLabel
        productDetailLongDescription.text = productDetailLongDescriptionLabel


    }

}

我无法继续使用我的代码,因为它一直给我一个错误: “无法将类型'[(String)]'的值赋给'String!'类型的值。

任何提示?谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

在TableViewController中:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var productDetailVC: ProductDetailViewController = segue.destinationViewController as! ProductDetailViewController
    if let selectedArrayIndex = tableView.indexPathForSelectedRow()?.row {
        productDetailVC.productDetailTitleLabel = shortDescription[selectedArrayIndex]
    }
}

在DetailsView中更改:

var productDetailTitleLabel = [String]()

var productDetailTitleLabel = String()

希望这有助于......:)