嗨,这是我第一次尝试使用搜索方法,我使用的是Apple Developer网站上的“搜索栏指南”,但是我遇到了这个问题,似乎无法弄清楚。我将数据存储在Class DataServices中,并且我的tableView单元在ViewController中工作得非常好,但是当我完成搜索时,似乎无法弄清楚应该在搜索方法中返回什么内容就像搜索指南中的说明一样,Xcode向我尖叫。你能用你的知识来祝福我
class ListVC: UIViewController,UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
var filteredData: [List]!
let data = DataServices.instance.getList()
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
tableView.dataSource = self
tableView.dataSource = self
filteredData = data
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataServices.instance.getList().count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ListingCell") as? ListCell {
let listing = DataServices.instance.getList()[indexPath.row]
cell.updateView(lists: listing)
return cell
}else {
return ListCell()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = searchText.isEmpty ? data: data.filter({ (List: String) -> Bool in
return List.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
> Value of type 'List' has no member 'range' is the error I get
})
tableView.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.resignFirstResponder()
}
}
DataServices类{ 静态let instance = DataServices()
private let currentList = [
List(firstText: "Name", secondText: "Shannon"),
List(firstText: "Car", secondText: "Infinity"),
List(firstText: "City", secondText: "Brandon"),
List(firstText: "Wife", secondText: "Naedra"),
List(firstText: "Child", secondText: "Shantae"),
List(firstText: "Job", secondText: "Driver"),
List(firstText: "Cell", secondText: "iPhone"),
List(firstText: "Computer", secondText: "Imac")
]
func getList() -> [List] {
return currentList
}
}
结构列表{
private (set) public var toptitle: String
private (set) public var bottomtitle: String
init(firstText: String, secondText: String) {
self.toptitle = firstText
self.bottomtitle = secondText
}
}
答案 0 :(得分:0)
闭包参数表示实例而不是类型,并且应用于数组的filter
闭包仅接受一个参数。空字符串检查是多余的。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = data.filter ({ (list) -> Bool in
return list.firstText.range(of: searchText, options: .caseInsensitive) != nil ||
list.secondText.range(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
或使用简化的语法
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = data.filter { $0.firstText.range(of: searchText, options: .caseInsensitive) != nil ||
$0.secondText.range(of: searchText, options: .caseInsensitive) != nil }
tableView.reloadData()
}
我建议将filteredData
声明为非可选的空数组,以避免不必要的拆包
var filteredData = [List]()
并强制向下转换单元格,如果代码崩溃,则表明存在设计错误。
let cell = tableView.dequeueReusableCell(withIdentifier: "ListingCell", for: indexPath) as! ListCell
如果您想在List
结构中使用常量,只需编写
struct List {
let toptitle: String
let bottomtitle: String
}
您免费获得初始化器。 private (set) var
是 objective-c-ish
最后每次调用cellForRow
都获得相同(静态)列表的方法不必要地昂贵,请从data
获取该项目
let listing = data[indexPath.row]