ios - TableView 上的搜索栏不起作用

标签 ios swift uitableview search uisearchbar

我正在尝试将搜索栏添加到一个简单的表格 View ,该表格 View 由 7 个名称单元格和每个名称的简短描述组成。

如图所示:

enter image description here

我在 swift 文件中创建了一个名为 Business 的类,它有两个属性:Name 和 Des。

这是 View Controller 中的代码:

  class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var TableView: UITableView!
var B = [Business]() //candies
var filteredNames = [Business]()

 let searchController = UISearchController(searchResultsController: nil)

func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredNames = B.filter { Bu in
        return Bu.Name.lowercaseString.containsString(searchText.lowercaseString)
    }

    TableView.reloadData()
}


override func viewDidLoad() {
    super.viewDidLoad()


    B = [
        Business(Name:"Mariah", Des:"I'm Here to help"),
        Business(Name:"Nada", Des:"Hi"),
        Business(Name:"Atheer", Des:"Hello"),
        Business(Name:"Lojian", Des:"I can Help you"),
        Business(Name:"Nadya", Des:"Hayat"),
        Business(Name:"Omnia", Des:"Yahoo"),
        Business(Name:"Eman", Des:"I have amazing stuff"),
        Business(Name:"Amani", Des:"Yess")
    ]

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    TableView.tableHeaderView = searchController.searchBar

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.active && searchController.searchBar.text != "" {
        return filteredNames.count
    }
    return B.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.TableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellTableViewCell

    cell.NameLabel.text = B[indexPath.row].Name
    cell.DescriptionLabel.text = B[indexPath.row].Des

    let Bu: Business

    if searchController.active && searchController.searchBar.text != "" {
        Bu = filteredNames[indexPath.row]
    } else {
        Bu = B[indexPath.row]
    }
     cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
     return cell
}


  }

 extension FirstViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController:   
(UISearchController)   {
filterContentForSearchText(searchController.searchBar.text!)
}
 }

我按照本教程来做到这一点: https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

我不知道为什么当我尝试在模拟器中搜索时结果总是第一个单元格:Mariah

代码有什么问题?

最佳答案

您不使用搜索结果来填充单元格。将 cellForRowAtIndexPath 替换为:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.TableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellTableViewCell

    let Bu: Business

    if searchController.active && searchController.searchBar.text != "" {
        Bu = filteredNames[indexPath.row]
    } else {
        Bu = B[indexPath.row]
    }

    cell.NameLabel.text = Bu.Name
    cell.DescriptionLabel.text = Bu.Des

    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    return cell
}

并且,不要对属性使用大写首字母。

关于ios - TableView 上的搜索栏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36699683/

相关文章:

ios - 导入文本和图像 swift Map Kit

应用程序运行时, Storyboard中设置的 iOS UITableView 行高加倍

swift - 我应该如何从 firebase 检索数据并将其放入字典中?

ios - URL 未在 webview 上加载

ios - 如何在 Swift 中将数据从外部数据库填充到 UItableView

iPhone:didSelectRowAtIndexPath 未调用

ios - 如何在 xcode 的单 View 应用程序中创建带有按钮的方向键

ios - 表格 View 单元格的圆角半径

iphone - 当转到另一个 UITableViewController 时 UITableViewController 卡住

ios - MBProgressHUD 禁用与 UITableViewController 的交互