2017-03-16 88 views
0

我正在使用swift 3中的項目,並且我有一個特定的UIViewController,我有一個UITableView,並且在其單元格上填充數據時,數據是從服務器獲取,並將其分配給JSON類型的數組。因此,我有一個UISearch欄放置在同一個UIViewController中的不同位置(而不是UITableView的頭部)。我的要求是實現搜索欄的功能,我可以過濾和搜索我的UITableView的數據。我怎樣才能做到這一點?儘管我沒有將代碼編寫成代碼,但我在代碼的一半上工作。如何在數組類型爲JSON時實現自定義搜索欄

import UIKit 
import SwiftyJSON 
import SDWebImage 


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate { 
    @IBOutlet weak var tableView: UITableView! 
    var count : Int = 0 
    var searchActive : Bool? 
    var selectedCategoryList = [JSON?]() 
    var filterSelectedCategoryList = [JSON?]() 
    let searchController = UISearchController(searchResultsController: nil) 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     passJson() 

     searchController.searchResultsUpdater = self 
     searchController.hidesNavigationBarDuringPresentation = false 
     searchController.dimsBackgroundDuringPresentation = false 
     definesPresentationContext = true 
     searchController.searchBar.sizeToFit() 
     self.tableView.tableHeaderView = searchController.searchBar 
     searchController.searchBar.delegate = self 
     self.tableView.reloadData() 

     // Do any additional setup after loading the view, typically from a nib. 
    } 




    func updateSearchResults(for searchController: UISearchController) { 




    } 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     filterSelectedCategoryList = selectedCategoryList.filter { titles in 
      return (titles?["title"].stringValue.lowercased().contains(searchText.lowercased()))! 
     } 
     tableView.reloadData() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func passJson() { 
     let path : String = Bundle.main.path(forResource: "JsonFile", ofType: "json") as String! 

     let jsonData = NSData(contentsOfFile: path) as NSData! 

     let readableJson = JSON(data: jsonData as! Data, options: JSONSerialization.ReadingOptions.mutableContainers, error: nil) 

     let json = readableJson 

     print(json) 

     let selectedItemArray = json["itemList"].arrayValue 
     self.selectedCategoryList = selectedItemArray 
     print("new prints",self.selectedCategoryList) 
     // print("Count",selectedItemArray?.count as Any) 


     self.count = self.selectedCategoryList.count 
     print(self.count) 


    } 


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


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if self.searchController.isActive && searchController.searchBar.text != ""{ 
      return filterSelectedCategoryList.count 
     }else{ 
      return selectedCategoryList.count 
     } 

    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell 
     if count > 0 { 
      if self.searchController.isActive && searchController.searchBar.text != ""{ 

       cell.label.text = filterSelectedCategoryList[indexPath.row]?["healerName"].stringValue 

      }else{ 
       cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue 
      } 


     } 
     return cell 
    } 



} 
+0

你的代碼有什麼問題?你想過濾什麼? – Poles

+0

不知道它不工作。程序崩潰後,點擊搜索欄 – danutha

+0

@danutha你可以顯示它崩潰的地方的屏幕截圖。 –

回答

1

嘗試實施的UISearch​Bar​Delegate方法,但設置searchBar.delegateselfviewDidLoad之前。

override func viewDidLoad() { 
    super.viewDidLoad() 
    passJson() 

    searchController.searchResultsUpdater = self 
    searchController.hidesNavigationBarDuringPresentation = false 
    searchController.dimsBackgroundDuringPresentation = false 
    definesPresentationContext = true 
    searchController.searchBar.sizeToFit() 

    //Set the delegate of searchBar 
    searchController.searchBar.delegate = self 

    self.tableView.reloadData() 
} 

現在實行的UISearchBarDelegatesearch​Bar(_:​text​Did​Change:​)方法和該方法篩選陣列。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
    filterSelectedCategoryList = selectedCategoryList.filter { titles in 
     return (titles?["healerName"].stringValue.lowercased().contains(searchText.lowercased()))! 
    } 
    tableView.reloadData() 
} 
+0

我刪除了「filterContentFroSearchedText」方法和「updateSearchResults」,然後用函數「searchBar」替換。雖然這次我的代碼沒有崩潰,但沒有提供搜索功能 – danutha

+0

是的,我做了,但它仍然沒有wokr – danutha

+0

@danutha用你正在嘗試的新代碼編輯你的問題。 –