我已经有一段时间了,我无法弄清楚如何将数据从表格视图单元格传递给新的"详细信息"控制器。我理解将它连接到新控制器的segue,但将该类中的变量设置为某些值失败(不显示值)。这就是我目前所拥有的:
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 :(得分: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对我来说很好看。如果您仍有问题,我会发布一个更完整的例子。