tableView不会显示包含搜索结果的单元格。搜索工作正常,我验证了所有数据,但是当我将cell.localName?.text分配给该值时,它始终为nil。不知道出什么问题了。我在Objective-C中发现了另一个具有相同问题的帖子,答案是我需要使用真正的tableView中的单元格,在使单元格出队时添加self.tableView,但这对我不起作用。有什么建议?
这是我的代码:
class SearchResultsController: UITableViewController, UISearchResultsUpdating {
var localNames: [String] = []
var filteredNames: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(localCell.self, forCellReuseIdentifier: "localCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchedString = searchController.searchBar.text
filteredNames.removeAll(keepCapacity: true)
if !searchedString.isEmpty {
let filter: String -> Bool = {name in
let range = name.rangeOfString(searchedString, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range != nil
}
let matches = localNames.filter(filter)
filteredNames += matches
}
tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return filteredNames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: localCell = self.tableView.dequeueReusableCellWithIdentifier("localCell") as localCell
cell.localName?.text = filteredNames[indexPath.row]
println(cell.localName?.text)
cell.localAddress?.text = "text"
cell.scheduleData?.text = "text"
cell.salaData?.text = "text"
cell.wifiData?.text = "text"
return cell
}
好的,所以我找到了解决方案并发布,以防其他人遇到与我相同的问题。我正在使用情节提要中的原型单元中的自定义单元。resultController创建一个新的tableView,它对自定义单元格一无所知,这就是为什么它总是返回nil的原因。为了解决这个问题,我引用了第一个使用的tableView而不是一个新的tableView,或者从一个真正的表中获取了该单元格,这样可以识别我的自定义单元格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句