【问题标题】:Search Bar on TableView Does not WorkTableView 上的搜索栏不起作用
【发布时间】:2016-04-18 16:41:19
【问题描述】:

我正在尝试向一个简单的表格视图添加一个搜索栏,该表格视图由 7 个名称单元格和每个名称的小描述组成。

如图所示:

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

这是视图控制器中的代码:

  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

代码有什么问题?

【问题讨论】:

  • 在您的代码中,您已经设置了 B 中的两个标签,然后再检查条件是来自过滤结果还是正常,dasdom 答案应该最适合您

标签: ios swift uitableview search uisearchbar


【解决方案1】:

您不使用搜索结果来填充单元格。将您的 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
}

而且,属性的首字母不要使用大写。

【讨论】:

  • 如何不使用大写字母?在哪一行代码中?
  • @IBOutlet weak var tableView: UITableView!var b = [Business]()
  • 但是为什么我不能在属性中使用大写字母呢?
  • 因为指南里是这么说的。
  • @Mariah 澄清一下,在使用包括 Swift 在内的许多语言进行编码时,标准做法是方法名称、属性名称和变量名称应以小写字母开头。类名应以大写字母开头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 2014-01-18
  • 2018-06-05
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多