如何在tableViewControllers之间传递模块数据?

时间:2015-05-19 19:57:31

标签: ios xcode swift

我试图将一个变量从一个tableViewController传递给另一个但是如何使用从模块收集的数据呢?

我想传递一个标题,一个描述和一个图像,但没有任何例外,我收到一条错误消息“使用未解析的标识符”

有人能找到我做错的事吗?

enter image description here

import UIKit

class DetailTableViewController: UITableViewController {

    var product: Product?
    @IBOutlet weak var matchImage: UIImageView!

    @IBOutlet weak var nameLabel: UILabel!

    @IBOutlet weak var descriptionLabel: UILabel!

    //MARK - VC life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

         matchImage.image = product?.image
         nameLabel.text = product?.title
         descriptionLabel.text = product?.description

         var matchTitle = product?.title

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var editVC: EditTableViewController = segue.destinationViewController as! EditTableViewController
    editVC.editName = matchTitle;

}
}

匹配控制器表视图控制器

import UIKit

class MatchesControllerTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = editButtonItem()
}

lazy var productLines: [ProductLine] = {
    return ProductLine.productLines()
}()


// MARK: - Table view data source

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let productLine = productLines[section]
    return productLine.name
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return productLines.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let productLine = productLines[section]
    return productLine.products.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MatchesCells", forIndexPath: indexPath) as! MatchesTableViewCells
    let productLine = productLines[indexPath.section]
    let product = productLine.products[indexPath.row]
    cell.configureCellWith(product)

    return cell
}

// MARK: - Edit Tableview
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete{
        let productLine = productLines[indexPath.section]
        productLine.products.removeAtIndex(indexPath.row)
        // tell the tableview
       tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let identifier = segue.identifier{
        switch identifier {
            case "detail view":
                let detailTableVC = segue.destinationViewController as! DetailTableViewController
                if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell){
                    detailTableVC.product = productAtIndexPath(indexPath)
            }

            default: break
        }
    }
}

func productAtIndexPath(indexPath: NSIndexPath) -> Product
{
    let productLine = productLines[indexPath.section]
    return productLine.products[indexPath.row]
}


}

MatchesTableViewCells表中的单元格

import UIKit

class MatchesTableViewCells: UITableViewCell {

@IBOutlet weak var productImageView: UIImageView!
@IBOutlet weak var productDescriptionLabel: UILabel!
@IBOutlet weak var productTitleLabel: UILabel!

func configureCellWith(product: Product)
{
    productImageView.image = product.image
    productDescriptionLabel.text = product.description
    productTitleLabel.text = product.title
}

}

EditTableViewController

import UIKit

class EditTableViewController: UITableViewController {

//Module:
var editName: String = ""
var editDescription: String = ""


@IBOutlet weak var nameLabel: UITextField!

@IBOutlet weak var matchDescriptionTextArea: UITextView!

@IBOutlet weak var matchImage: UIImageView!


//MARK - VC life cycle
override func viewDidLoad() {
    super.viewDidLoad()

    //matchImage.image = editImg
    nameLabel.text = editName
    matchDescriptionTextArea.text = editDescription


}
}

1 个答案:

答案 0 :(得分:0)

您的matchTitle是在viewdidload加载中创建的,prepareForSegue

中不再存在上下文
import UIKit

class DetailTableViewController: UITableViewController {

    var product: Product?

    @IBOutlet weak var matchImage: UIImageView!

    @IBOutlet weak var nameLabel: UILabel!

    @IBOutlet weak var descriptionLabel: UILabel!

    //MARK - VC life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

         matchImage.image = product?.image
         nameLabel.text = product?.title
         descriptionLabel.text = product?.description  
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var editVC: EditTableViewController = segue.destinationViewController as! EditTableViewController
    editVC.editName = product?.title;
    //Assuming editName is a string, if it is a UILabel it should be
    //editVC.editName.text = product?.title;
    //If editName is a string and product?.title a UITextField
    //editVC.editName = product?.title.text;
}
}