首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchDisplayController错误- Swift

UISearchDisplayController错误- Swift
EN

Stack Overflow用户
提问于 2015-11-06 10:12:08
回答 1查看 980关注 0票数 0

我正在跟踪这个教程,我遇到了一些麻烦。我正在实现一个搜索栏和显示控制器来过滤结果。

下面是我所得到的错误的图片:

主要错误是:

  1. UISearchDisplayController被否决了
  2. 不能下标字符串类型的值。

任何帮助,以解决这些问题将是有帮助的。

下面是我的其余代码:

代码语言:javascript
复制
    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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-06 11:17:37

  1. UISearchDisplayController被否决了

UISearchDisplayController在iOS 8中被否决了,您可以使用UISearchController,它非常类似于UISearchDisplayController。例如,请查看本教程:http://www.jhof.me/simple-uisearchcontroller-implementation/

  1. 不能下标字符串类型的值。

scopeButtonTitles返回可选值,因此scopes是可选的。可选数组必须在下标之前展开,强制以这种方式展开:

代码语言:javascript
复制
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!, scope: scope![searchOption])

或者使用任何附加的可选检查逻辑:

代码语言:javascript
复制
if let scope = scope {        
    self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!, scope: scope[searchOption])
} else {
// handle error
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33564322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档