繁体   English   中英

如何将解析数据从TableView传递到详细信息视图控制器?

[英]How to pass parse data from tableview to detail view controller?

解析后端和一起编码的新功能...

希望通过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!'的值。

有小费吗? 谢谢!

尝试这个:

在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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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