2014-10-06 28 views
0

我一直在这一段时间,我无法弄清楚如何将数据从表格视图单元传递到新的“细节”控制器。我了解将它连接到新控制器的segue,但将该类中的变量设置为某些值失败(不显示值)。这是我目前有:将数据从UITableView传递到新控制器,SWIFT

import UIKit 

class BuyTreeViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate { 

    var products : [Product] = [] 
    var filteredProducts = [Product]() 

    override func viewDidLoad() { 

     self.products = [ 
     Product(name:"Chocolate", price: 11, description: "i"), 
     Product(name:"Candy", price: 12, description: "h"), 
     Product(name:"Break", price: 13, description: "g"), 
     Product(name:"Apple", price: 14, description: "f"), 
     Product(name:"Computer", price: 15, description: "e"), 
     Product(name:"Laptop", price: 16, description: "d"), 
     Product(name:"Cup", price: 17, description: "c"), 
     Product(name:"Table", price: 18, description: "b"), 
     Product(name:"Chair", price: 19, description: "a") 
     ] 

     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     return self.filteredProducts.count 
    } else { 
     return self.products.count 
    } 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell 

    var product : Product 
    // Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     product = filteredProducts[indexPath.row] 
    } else { 
     product = products[indexPath.row] 
    } 

    // Configure the cell 
    cell.textLabel!.text = product.name 
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 

    return cell 
    } 

    func filterContentForSearchText(searchText: String, scope: String = "All") { 
    self.filteredProducts = self.products.filter(
     { 
     (product : Product) -> Bool in 
     var categoryMatch = (scope == "All") || (product.category == scope) 
     var stringMatch = product.name.rangeOfString(searchText) 
     return categoryMatch && (stringMatch != nil) 
    }) 
    } 

    func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool { 
    let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String] 
    let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String 
    self.filterContentForSearchText(searchString, scope: selectedScope) 
    return true 
    } 

    func searchDisplayController(controller: UISearchDisplayController!, 
    shouldReloadTableForSearchScope searchOption: Int) -> Bool { 
     let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String] 
     self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption]) 
     return true 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier("productDetail", sender: tableView) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if segue.identifier == "productDetail" { 
//  let productDetailViewController = segue.destinationViewController as UIViewController 
     let productDetailViewController = segue.destinationViewController as ProductDetailViewController 
     productDetailViewController.testLabel.text = "123345" 

     if sender as UITableView == self.searchDisplayController!.searchResultsTableView { 
     let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()! 
     let destinationTitle = self.filteredProducts[indexPath.row].name 
     productDetailViewController.title = destinationTitle 
     } else { 
     let indexPath = self.tableView.indexPathForSelectedRow()! 
     let destinationTitle = self.products[indexPath.row].name 
     productDetailViewController.title = destinationTitle 
     } 


     productDetailViewController.productPriceText = "10" 
     productDetailViewController.productTitleText = "100" 
     productDetailViewController.productDescriptionText = "100" 
    } 
    } 

这是我在ProductDetailViewController:

import UIKit 

class ProductDetailViewController: UIViewController { 

    @IBOutlet weak var productDescription: UITextView! 
    @IBOutlet weak var productPrice: UITextField! 
    @IBOutlet weak var productTitle: UITextField! 
    @IBOutlet weak var connectUserButton: UIButton! 
    @IBOutlet weak var testLabel: UILabel! 

// var productDescriptionText: String 
// var productPriceText: String 
// var productTitleText: String 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
//  productDescription.text = productDescriptionText 
//  productPrice.text = productPriceText 
//  productTitle.text = productTitleText 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 
    @IBAction func contactUserClicked(sender: AnyObject) { 
    println("conentUserButton clicked") 
    } 

回答

0

为了您prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
     if segue.identifier == "productDetail" { 

     let indexPath = self.tableView.indexPathForSelectedRow() 
     let theSelectedRow = filteredProducts[indexPath!.row] 
     let theDestination = segue.destinationViewController as ProductDetailViewController 

     theDestination.productPriceText = "10" 
     theDestination.productTitleText = "100" 
     theDestination.productDescriptionText = "100" 

    } else 

     //only needed if more than one segue 
    } 
} 

你detailViewController看着我好。如果你仍然有问题,我会发布一个更完整的例子。

+0

此行,theDestination.testLabel.text =“123345”将不起作用。在prepareForSegue时,细节控制器已经实例化,但它的视图没有加载,所以它的出口将是空的。 – rdelmar 2014-10-06 02:24:45

+0

哎呀,在一分钟内修好了 – 2014-10-06 02:36:59

+0

不幸的是,这没有奏效。我无法设置它的文本,因为它是零。有没有其他方法可以在下一个视图中设置标签? @SteveRosenberg – user3698231 2014-10-06 16:30:42

相关问题