我创建了继“从iPhone开发与雨燕” book.The搜索栏的tableView一个TableView中的应用程序与代码,而不是内部的storyboard.The本书解释了如何让搜索创建结果,并显示相应的细胞,但我想我的应用程序来执行原因请看我在storyBoard.How创建视图控制器我就会触发代码Segue公司?如何使我的搜索栏的TableView执行赛格瑞
更多的信息,这是我的文件:
import UIKit
class SearchResultsController: UITableViewController , UISearchResultsUpdating{
let sectionsTableIdentifier = "section identifier"
var products = [product]()
var filteredProducts = [product]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: sectionsTableIdentifier)
}
// MARK: - Table view data source
override func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return filteredProducts.count
}
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(
sectionsTableIdentifier) as UITableViewCell
cell.textLabel!.text = filteredProducts[indexPath.row].name
return cell }
// 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?) {
if segue.identifier == "detailView"{
let index = self.tableView?.indexPathForSelectedRow()
var destinationViewController : infoViewController = segue.destinationViewController as infoViewController
destinationViewController.Title = filteredProducts[index!.row].title
destinationViewController.eam = filteredProducts[index!.row].energy
destinationViewController.fam = filteredProducts[index!.row].fat
destinationViewController.pam = filteredProducts[index!.row].protein
destinationViewController.cam = filteredProducts[index!.row].carbohydrates
destinationViewController.imgName = filteredProducts[index!.row].imgName
}
}
func updateSearchResultsForSearchController(
searchController: UISearchController) {
let searchString = searchController.searchBar.text
filteredProducts.removeAll()
for prod in products{
var name = prod.name.lowercaseString
if name.rangeOfString(searchString) != nil {
filteredProducts.append(prod)
}
}
tableView.reloadData()
}}
只是为了确认,是SearchResultsController实例本身在代码中创建,或者仅仅的tableView?也是在代码中创建 – pbasdf 2014-12-27 21:02:35
的SearchResultsController,在其他类:'让resultsController = SearchResultsController() resultsController.products = self.produits searchController = UISearchController(searchResultsController:resultsController) 让搜索栏= searchController.searchBar searchBar.placeholder = “输入食物名称” searchBar.sizeToFit() tableView.tableHeaderView = searchBar searchController.searchResultsUpdater = resultsController' – 2014-12-28 07:13:39