ios - UISearchDisplayController 错误 - Swift

标签 ios swift swift2 uisearchdisplaycontroller uisearchcontroller

我正在关注这个tutorial我遇到了一些麻烦。 我正在实现搜索栏和显示 Controller 来过滤结果。

下面是我遇到的错误的图片:

enter image description here

主要错误是:

  1. UISearchDisplayController 已弃用
  2. 无法下标 [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/

  1. 无法下标 [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
}

关于ios - UISearchDisplayController 错误 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33564322/

相关文章:

ios - 单击 webViewKit 按钮的用户权限

iOS tableview 如何检查它是向上还是向下滚动

ios - 存储的属性需要一个初始值或者应该是@NSManaged

cocoa - NSURLSessionDataTask 在简单的 HTTPS URL 上失败并出现错误 -1002

ios - XCode 7/Swift 2 的字典类型速记语法错误

ios - 任何 iOS 应用程序如何从解析中下载视频? swift

javascript - iPhone touchmove绕过

ios - 如果初始帧为 CGRectZero,则自定义 UIView 的 drawRect 永远不会被调用

ios - 点击 super View 中嵌入的自定义 UIView 中未收到的手势识别器

ios - Swift 中的像素数组到 UIImage