TableView搜索栏冻结

时间:2015-02-23 06:49:10

标签: ios uitableview swift

使用标准搜索栏设置我的tableview。最多可以搜索1000个值(这不应该太昂贵)。

问题:如果输入单个字符,搜索栏将滞后30-90秒.CPU和内存为100%。我是否错误地实施了搜索栏?

TableView Cell

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("RestaurantCell") as UITableViewCell
    var name : Name
    // Check to see whether search results table is being displayed
    if tableView == self.searchDisplayController!.searchResultsTableView {
        name = self.filteredNames[indexPath.row]
    } else {
        name = self.names[indexPath.row]
    }

    cell.textLabel?.text = name.name
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    return cell
}

过滤代码

func filterContentForSearchText(searchText: String, scope: String = "All") {
    self.filteredNames = self.names.filter({( name : Name) -> Bool in
        var categoryMatch = (scope == "All") || (name.city == scope)
        var stringMatch = name.name.rangeOfString(searchText)
        self.filterRestaurants(searchText)
        return categoryMatch && (stringMatch != nil)
    })
}

func filterRestaurants(filteredObject: String) {
    selectFilteredRestaurantArray = self.selectRestaurantArray.filter()  {
        if let type = ($0 as PFObject)["Name"] as? String {
            return type.rangeOfString(filteredObject) != nil
        } else {
            return false
        }
    }
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    self.filterContentForSearchText(searchString)
    return true
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
    self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
    return true
}

0 个答案:

没有答案