SearchableTableView是一种快速向表格视图添加搜索机制的方法。
只需将SearchableTableView.swift复制到您的项目,并用它来代替您的UITableView。或者使用CocoaPods。
CocoaPods是Cocoa项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
使用CocoaPods将SearchableTableView集成到您的Xcode项目中,在您的Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'SearchableTableView'
然后,运行以下命令
$ pod install
请记住,它使用的是Swift。所以您需要使用CocoaPods的框架以这种方式嵌入它。
要使用SearchableTableView,只需将其用于UITableView即可。
要检查用户是否正在进行搜索,请检查您的tableView的searchQuery属性。最简单的方法是在控制器中实现一个附加的检查函数,如
private func updateValues() {
if tableView.searchQuery != nil && tableView.searchQuery!.characters.count > 0 {
if lastQuery == nil || lastQuery != tableView.searchQuery {
lastQuery = tableView.searchQuery
currentItems = items.filter({ (item) -> Bool in
return item.lowercaseString.containsString(tableView.searchQuery!.lowercaseString)
})
}
} else {
lastQuery = nil
currentItems = items
}
}
然后在table view请求您提供值时运行它。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
updateValues()
return currentItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
updateValues()
var cell = tableView.dequeueReusableCellWithIdentifier("TestCell")
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: "TestCell")
}
cell?.textLabel?.text = currentItems[indexPath.row]
return cell!
}
SearchableTableView由Nova Project拥有和维护。
Alamofire是在MIT许可下发布的。有关详细信息,请参阅LICENSE。