我按照“使用 Swift 开始 iPhone 开发”一书创建了一个 TableView 应用程序。搜索栏 tableView 是用代码创建的,而不是在 Storyboard 中创建的。这本书解释了如何获取搜索结果并显示相应的单元格,但我会就像我的应用程序对我在 Storyboard中创建的 ViewController 执行转场一样。如何使用代码触发转场?
有关更多信息,这是我的文件:
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()
}}
最佳答案
因为 Controller 是在代码中构建的,所以您需要使用 SearchResultsController
的 tableView 委托(delegate)方法 didSelectRowAtIndexPath
来触发下一个 View Controller 的呈现。
假设有一个支持 SearchResultsController
的 TableView Controller ,您可以将其用作 SearchResultsController
的委托(delegate)。主 TableView Controller 可能已经有必要的代码来在选择单元格时进行 segue,在这种情况下,您需要检查选择了哪个 tableView,以便正确确定该单元格代表的产品。
要设置委托(delegate),请将以下行添加到创建 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
...
}
}
如果主 TableView Controller 在 Storyboard中,您可以使用 segue 来呈现细节 VC。在这种情况下,您将在上面的代码中使用 self.performSegueWithIdentifier()
。如果没有,您可以使用 self.navigationController?.pushViewController()
(如果您嵌入在导航 Controller 中)或 self.presentViewController()
(显示详细信息模态 VC)。
另一种选择是将 SearchResultsController
的 delegate
设置为 self
(在 viewDidLoad
中),然后在 SearchResultsController
类中实现 didSelectRowAtIndexPath
。在这种情况下,您不需要测试哪个 tableView 触发了该方法,但您将无法使用 segue。
关于ios - 如何让我的 searchBar TableView 执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27669956/