我有一个UITableViewController(具有6个单元格)和一个UISearchController。每当我点击搜索栏时,表格视图单元格都会消失,并被一个空的tableView取代,直到我开始输入内容并出现搜索结果为止。当我点击搜索栏时,如何使单元格留在下面?
这是我与SearchController有关的代码:
在viewDidLoad()中:
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = true
controller.searchBar.sizeToFit()
controller.searchBar.barTintColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
return controller
})()
其余的部分:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredTableData.count
} else {
return self.items.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
var item : TextCell
if (self.resultSearchController.active) {
item = filteredTableData[indexPath.row]
return cell
} else {
item = items[indexPath.row]
cell.userText.text = item.userText
cell.userPreferredName.text = item.userPreferredName
cell.userName.text = item.userName
cell.userImage.layer.cornerRadius = 23.75
cell.userImage.clipsToBounds = true
cell.userImage.layer.borderWidth = 0.3
cell.userImage.layer.borderColor = UIColor.lightGrayColor().CGColor
cell.time.text = "\((indexPath.row + 1) * 3)m"
return cell
}
}
}
extension TableViewController: UITableViewDelegate {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
extension TableViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
var filteredArray : [TextCell] = [TextCell]()
for textCell in self.items
{
if (textCell.userName.rangeOfString(searchController.searchBar.text) != nil ||
textCell.userPreferredName.rangeOfString(searchController.searchBar.text) != nil ||
textCell.userText.rangeOfString(searchController.searchBar.text) != nil)
{
filteredArray.append(textCell)
}
}
filteredTableData = filteredArray
self.tableView.reloadData()
}
}
屏幕截图:
奖金:
有什么办法可以使这些单元格变为可选状态?
最佳答案
如果searchText为空,则将filteredTableData
设置为items
extension TableViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
var filteredArray : [TextCell] = [TextCell]()
if searchController.searchBar.text == "" {
filteredArray = self.items
} else {
for textCell in self.items
{
if (textCell.userName.rangeOfString(searchController.searchBar.text) != nil ||
textCell.userPreferredName.rangeOfString(searchController.searchBar.text) != nil ||
textCell.userText.rangeOfString(searchController.searchBar.text) != nil)
{
filteredArray.append(textCell)
}
}
}
filteredTableData = filteredArray
self.tableView.reloadData()
}
}