1

New to parse backend and coding all together...

Looking to pass data from my HomeTimelineViewController(VC #1), to my ProductDetailViewController(VC #2) through the UIStoryboard segue.

Here is my code for 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
    }   
}

Here is my code for 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


    }

}

I can't continue with my code because it keeps giving me an error: "Cannot assign a value of type '[(String)]' to a value of type 'String!'.

Any tips? Thank you!

V1P3R
  • 176
  • 3
  • 16

1 Answers1

0

Try this:

In 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]
    }
}

In DetailsView change this:

var productDetailTitleLabel = [String]()

to

var productDetailTitleLabel = String()
halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73