【发布时间】:2018-12-23 01:04:04
【问题描述】:
您好,这是我第一次尝试使用搜索方法,我正在使用 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()
}
}
类数据服务 { static 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
}
}
【问题讨论】:
-
请编辑您的问题以显示
List的定义 - 您想要$0.someProperty其中someProperty是您要过滤的字符串属性。您现在拥有的代码正在尝试在List类上调用range。 -
另外,
DataServices.instance.getList()是否执行网络操作?如果是这样,那么您不应该重复这样做 - 您应该获取一次数据 -
DataServices.instance.getList() 来自我存储数据的 swift 文件,这里是代码
-
我添加了带有代码的文件以显示更多信息