2016-04-18 88 views
1

我正在尝试将搜索栏添加到由7个单元格组成的简单表格视图,这些单元格包含名称和每个名称的小描述。TableView上的搜索栏不起作用

由于这里的图像:

enter image description here

我取得了迅速的文件中的类称为Business,即有两个属性:名称和德。

下面是视图控制器代码:

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

我不知道whay当我试图在模拟器搜索的结果总是第一个单元格:Mariah

代码有什么问题?

+0

在你的代码中,如果你从过滤结果或正常情况检查条件之前,你已经设置了两个标签,dasdom答案应该主要适用于你 – HardikDG

回答

0

您不使用搜索结果填充单元格。更换你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 
} 

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

+0

我怎样才能不使用大写字母?在哪行代码? – Mariah

+0

'@IBOutlet weak var tableView:UITableView!'和'var b = [Business]()'。 – dasdom

+0

但为什么我不应该使用大写字母作为属性? – Mariah