2017-09-13 52 views
0

我有一個工作搜索欄,現在我只需要幫助在搜索文本與數組中的任何項目不匹配時清除表格(不包括空的搜索欄)。搜索欄過濾器在結果不匹配時不清除表格

我還想爲其中一個單元格顯示一條消息,當沒有找到匹配(如「沒有結果可用」)。

這裏是我的代碼:

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

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() 

    self.navigationController?.setNavigationBarHidden(true, animated: false) 

    AppState.shared.category = "Alphabetical" 

} 

@IBAction func backButton(_ sender: Any) { 

    if let navController = self.navigationController { 
     for controller in navController.viewControllers { 
      if controller is ToolsViewController { 
       navController.popToViewController(controller, animated: true) 
      } 
     } 
    } 
} 

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

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

    searchActive = !filtered.isEmpty 

    self.toolTable.reloadData() 

} 

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

    return 1 
} 

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

    if(searchActive) { 
     return filtered.count 
    } 

    return items.count 

} 

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

    cell.alphabeticalLabel.layer.masksToBounds = true 

    if(searchActive) { 
     cell.alphabeticalLabel.text = filtered[indexPath.row].data 
     cell.alphabeticalLabel.layer.cornerRadius = 10 
    } else { 
     cell.alphabeticalLabel.text = items[indexPath.row].data 
     cell.alphabeticalLabel.layer.cornerRadius = 10 
    } 

    return cell 
} 

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) 
} 
} 

我知道這是不是創建搜索欄功能的最佳途徑,但是這是我一直在使用了一段時間。我相信解決方案不是很複雜,但我只是沒有任何運氣。

任何幫助將不勝感激。

+0

我假設其他邏輯按照您的預期工作。如果'!searchActive'而不是'items.count',爲什麼不在'numberOfRowsInSection'中返回0? 或者返回1,然後在'cellForRowAtIndexPath'中設置單元格的'alphabeticalLabel'爲「找不到結果」? – leandrodemarco

+0

@leandrodemarco,問題是,如果我這樣做了,那麼在開始時表格是空白的,直到我鍵入內容爲止。表格應該爲空的唯一類型是在搜索欄中鍵入了不正確的內容時。 –

回答

1

根據您的回覆,加上檢查,看是否搜索文本是否爲空:

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

    if(searchActive) { 
     return filtered.count 
    } 
    else if (searchForTool.text? == "") { // check if this is "" or nil 
     return items.count 
    } 
    else { 
     return 0 // Or 1 if you want to show a cell with "no found" text 
    } 
} 

你需要調整cellForRowAtIndexpath類似。當用戶沒有輸入任何東西時,搜索欄的文本屬性爲零或爲空字符串時檢查。

+0

是的,效果很好。我只需要將searchForTool.text的選項關閉,否則會出錯。我相應地更正了cellForRowAtIndexpath。謝謝! –