2017-09-01 77 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實際上是從我所能看到的。謝謝! –