UISearchDisplayController 错误 - 斯威夫特
Posted
技术标签:
【中文标题】UISearchDisplayController 错误 - 斯威夫特【英文标题】:UISearchDisplayController Error - Swift 【发布时间】:2015-11-06 10:12:08 【问题描述】:我正在关注这个tutorial,但遇到了一些麻烦。 我正在实现一个搜索栏和显示控制器来过滤结果。
下面是我遇到的错误的图像:
主要错误有:
-
UISearchDisplayController 已弃用
不能为 [String] 类型的值下标?
任何解决这些问题的帮助都会有所帮助。
以下是我的其余代码:
import UIKit
class TableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate
var candies = [Candy]()
var filteredCandies = [Candy]()
var resultSearchController = UISearchController()
override func viewDidLoad()
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.candies = [Candy(category:"Chocolate", name:"Chocolate Bar"),
Candy(category:"Chocolate", name:"chocolate chip"),
Candy(category:"Hard", name:"lollipop"),
Candy(category:"Hard", name:"candy cane"),
Candy(category:"Other", name:"caramel"),
Candy(category:"Chocolate", name:"chocolate chip")]
self.tableView.reloadData()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func filterContentForSearchText(searchText: String, scope: String = "All")
self.filteredCandies = self.candies.filter((candy: Candy) -> Bool in
let categoryMatch = ( scope == "All") || (candy.category == scope)
let stringMatch = candy.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
)
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool
let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles
let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
self.filterContentForSearchText(searchString, scope: selectedScope)
return true
func searchController(controller: UISearchController, shouldReloadTableForSearchScope searchOption: Int) -> Bool
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!, scope: scope[searchOption])
return true
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
// #warning Incomplete implementation, return the number of sections
return 1
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// #warning Incomplete implementation, return the number of rows
if tableView == searchDisplayController!.searchResultsTableView
return self.filteredCandies.count
else
return self.candies.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
//let candy = self.candies[indexPath.row]
var candy : Candy
if tableView == self.searchDisplayController!.searchResultsTableView
candy = filteredCandies[indexPath.row]
else
candy = candies[indexPath.row]
cell.textLabel?.text = candy.name
return cell
【问题讨论】:
【参考方案1】:-
UISearchDisplayController 已弃用
UISearchDisplayController 在 ios 8 中已被弃用。您可以使用UISearchController
,它与UISearchDisplayController
非常相似。例如,查看本教程:http://www.jhof.me/simple-uisearchcontroller-implementation/
-
不能为 [String] 类型的值下标?
scopeButtonTitles
返回可选值,所以scopes
是一个可选值。可选数组必须在下标之前解包,以这种方式强制解包:
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!, scope: scope![searchOption])
或任何额外的可选检查逻辑:
if let scope = scope
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!, scope: scope[searchOption])
else
// handle error
【讨论】:
以上是关于UISearchDisplayController 错误 - 斯威夫特的主要内容,如果未能解决你的问题,请参考以下文章
为啥 UISearchDisplayController 有时有效,有时无效?
iOS UISearchDisplayController学习笔记
在 UISearchDisplayController 上遇到僵尸问题
修复 UITableView 顶部的 UISearchdisplaycontroller 搜索栏