如何自定义 UITableView 中的搜索功能

How to customise the search feature in UITablView

在搜索栏结果中,我想先使用 AnchoredSearch 选项进行比较,如果我没有在其中获得值,那么我想仅使用 CaseInsensitiveSearch 选项进行比较。

我在下面附上了我的搜索栏代码。

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

        self.array = self.getUniqArrayData(self.array)
        filteredTableData = array.filter({ (text) -> Bool in
            let tmp: NSString = text
            var range = tmp.rangeOfString(searchText, options: (NSStringCompareOptions.AnchoredSearch | NSStringCompareOptions.CaseInsensitiveSearch ))

            return range.location != NSNotFound
        })

        if(searchText == ""){

            searchActive = false;
        } else {
            searchActive = true;
        }
        self.xyztable.reloadData()
    }

请告诉我如何先使用 AnchoredSearch 进行过滤,如果我没有在其中找到任何内容,然后使用 CaseInsensitiveSearch 选项进行搜索。

任何示例或示例代码或链接都会有所帮助

这是简单的排序。我已经清理了一些东西; tmp 是不必要的。

// I don't think you intend to overwrite the underlying data just because
// the user did a search. Did you mean to do a local variable:
let uniqArray = self.getUniqArrayData(self.array)
let tryAnchored = uniqArray.filter { (text) -> Bool in
    var range = text.rangeOfString(searchText, options: .AnchoredSearch)
    return range.location != NSNotFound
}
if tryAnchored.count > 0 {
    self.filteredTableData = tryAnchored
}
else {
    // maybe have a local 'let' here too, and if this one also comes up
    // empty, don't reload the table data at all?
    self.filteredTableData = uniqArray.filter { (text) -> Bool in
        var range = text.rangeOfString(searchText, options: .CaseInsensitiveSearch)
        return range.location != NSNotFound
    }
}