我最近使用了Add Search to Table View Tutorial in iOS8 with Swift的教程,它很好用,很容易理解。我遇到的问题是这只允许你搜索文本。在我的textViewCells中,我也有detailTextLabel
和accessoryView
这是一个图像。这两个我想在输入文本后成为搜索单元的一部分。目前,在元素0的tableViewCell中的任何内容都与名称保持一致。用于更改的代码更新搜索控制器:
func updateSearchResultsForSearchController(searchController: UISearchController){
filteredMatchesNames.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let arrayName = (finalMatchesName as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredMatchesNames = arrayName as! [String]
self.tableView.reloadData()
}
以及tableView
cellForRowAtIndexPath
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
var imageView = UIImageView(frame: CGRectMake(cell.frame.origin.x + cell.frame.width - cell.frame.height - 5, 5, cell.frame.height - 10, cell.frame.height - 10))
if (self.resultSearchController.active) {
var dis = ["2222", "4444"]
var images = UIImage(data: finalMatchesImage[indexPath.row])
imageView.image = images
cell.textLabel?.text = filteredMatchesNames[indexPath.row] //This works fine
cell.detailTextLabel?.text = dis[indexPath.row] //this doesn't work
cell.accessoryView = imageView //This doesn't work
return cell
}
else {
var images = UIImage(data: finalMatchesImage[indexPath.row])
imageView.image = images
var dis = ["2222", "444"]
cell.textLabel?.text = finalMatchesName[indexPath.row] //This works fine
cell.detailTextLabel?.text = dis //This works fine
cell.accessoryView = imageView //This works fine
return cell
}
}
完整的代码在链接中,虽然我认为问题应该只在cellForRowAtIndexPath
内,如果我可以这样做,那么搜索整个单元格而不仅仅是文本。