2014-12-27 61 views
1

我创建了继“从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() 
}}  
+0

只是为了确认,是SearchResultsController实例本身在代码中创建,或者仅仅的tableView?也是在代码中创建 – pbasdf 2014-12-27 21:02:35

+0

的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

回答

0

由于控制器内置的代码,你需要使用SearchResultsController的的tableView委托方法didSelectRowAtIndexPath触发下一个视图控制器的呈现。

假设有一个支持SearchResultsController的表格视图控制器,您可以使用它作为SearchResultsController的代表。主表视图控制器可能已经具备必要的代码,当一个单元被选择,在这种情况下,你需要检查它的tableView是为了正确地确定电池代表产品被选中Segue公司。

要设置委托,下面一行添加到代码(在上面您的评论),您创建SearchResultsController

resultsController.tableView?.delegate = self 

然后修改didSelectRowAtIndexPath方法来测试它的tableView被触发方法:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if (tableView == self.tableView) { 
     // use the existing code to present the detail VC, based on the data in the main table view 
     ... 
    } else { 
     // use new code to present the detail VC, based on data from the SearchResultsController 
     ... 
    } 
} 

如果主表视图控制器在故事板中,则可以使用segue来呈现细节VC。在这种情况下,您可以在上面的代码中使用self.performSegueWithIdentifier()。如果没有,则要么使用self.navigationController?.pushViewController()(如果要嵌入在导航控制器)或self.presentViewController()(以呈现细节VC模态)。

另一种选择是设置SearchResultsControllerdelegateself(在viewDidLoad),然后执行在SearchResultsControllerdidSelectRowAtIndexPath。在这种情况下,您不需要测试哪个tableView触发了该方法,但是您将无法使用segue。

+0

我试过你的方法,会发生什么情况是,当我在searchBar中键入关键字并且出现过滤的单元格时点击其中一个单元格没有任何反应,当我按下searchBar的取消按钮时,我终于看到detailView .So基本上它就像SEGUE在后台完成,并且没有明显的赛格瑞它在后台完成,你可以看到的DetailView只有一次,你取消了research.I希望您能理解。 – 2014-12-29 07:24:06

+0

我了解您的描述,但我无法立即解释它。你有没有在我的最后一段中尝试过这个选项 - 将SearchResultController的tableView委托设置为self,并在该类中实现didSelectRowAtIndexPath? – pbasdf 2014-12-29 10:04:24

+0

测试了一下后,一个解决方法(在我的原始解决方案中)是在执行segue后更改UISearchController的'active'属性 - 在'didSelectRowAtIndexPath'方法末尾添加'searchController.active = false'。 – pbasdf 2014-12-29 23:36:55