2017-09-01 96 views
0

我在我的tableview中有一个搜索栏,但是当我最初单击搜索栏时,结果消失。如果我继续使用其他控制器并返回,搜索栏会正常工作,所有结果都会显示单击栏的时间。搜索栏未正确过滤

下面是代码:

@IBOutlet weak var toolTable: UITableView! 
@IBOutlet weak var searchForTool: UISearchBar! 

var searchActive : Bool = false 
{ 
    didSet { 
     if searchActive != oldValue { 
      toolTable?.reloadData() 
     } 
    } 
} 

typealias Item = (data: String, identity: String) 

var filtered: [Item] = [] 
var items: [Item] = [ 
    (data: " Data1", identity: "A"), 
    (data: " Data2", identity: "B") 
] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    toolTable.delegate = self 
    toolTable.dataSource = self 

    searchForTool.delegate = self 

} 

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 
    searchActive = true 
} 

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { 
    searchActive = false 
} 

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
    searchActive = false 
} 

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 
    searchActive = false 
} 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 

    filtered = items.filter { item in 
     item.data.localizedCaseInsensitiveContains(searchText) 
    } 

    searchActive = !filtered.isEmpty 

    self.toolTable.reloadData() 
} 


override func numberOfSections(in tableView: UITableView) -> Int { 

    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if(searchActive) { 
     return filtered.count 
    } 

    return items.count 

} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell 

    if(searchActive){ 
     cell.toolLabel.text = filtered[indexPath.row].data 
    } else { 
     cell.toolLabel.text = items[indexPath.row].data 
    } 

    return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let vcName: String 
    if searchActive { 
     vcName = filtered[indexPath.row].identity 
    } else { 
     vcName = items[indexPath.row].identity 
    } 
    let viewController = storyboard?.instantiateViewController(withIdentifier: vcName) 
    self.navigationController?.pushViewController(viewController!, animated: true) 

} 

我敢肯定它的一些简单的解决办法,我只是俯瞰它。

任何帮助将不胜感激。

+2

你为什么不使用UISearchController? – matt

+0

这只是我学会创建搜索栏功能的第一种方式。 –

+0

我可以说服你花时间以正确的方式做到这一点吗? UISearchController存在的原因是这样的。 – matt

回答

1

读取您的代码我看到:searchBarTextDidBeginEditingsearchActive设置为true。数据将按照didSet中的代码重新加载。然后调用tableView:numberOfRowsInSection并返回filtered.count,意思是0,因为它是空的。这就是结果消失的原因。

+0

啊我明白了。解决问题的最简单方法是什么?我已经尝试了几件事,但是我得到索引超出范围错误。 –

+1

@SlavictheSlavic你可以尝试删除'searchBarTextDidBeginEditing'方法。我不确定它需要在这里。如果它不起作用,你可以看看这个项目的'SoundListTableViewController':https://github.com/nyg/iOSSystemSoundsLibrary。从截图中可以看到,它还使用了一个没有'UISearchController'的搜索栏。 – nyg

+0

删除searchBarTextDidBeginEditing实际上是从我所能看到的。谢谢! –