簡體   English   中英

TableView上的搜索欄不起作用

[英]Search Bar on TableView Does not Work

我正在嘗試將搜索欄添加到一個簡單的表視圖中,該視圖由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

我不知道當我試圖在模擬器中搜索結果總是第一個單元格時:瑪麗亞

代碼有什么問題?

您不使用搜索結果來填充單元格。 用這個替換你的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
}

並且,不要使用大寫首字母作為屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM