2017-03-20 52 views
0

我正在使用swift 3中的項目,並且我有一個特定的UIViewController,我有一個UITableView,並且在其單元格中填充數據時,數據從服務器獲取,它是一個JSON類型的數組。我想實現一個自定義的UISearchBar來過濾這些數據。我已經部分地使用了代碼,並且它的內容如下所示。未在自定義UISearchBar中獲取過濾器數據

import UIKit 
import SwiftyJSON 
import SDWebImage 


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate {  

    @IBOutlet weak var tableView: UITableView! 
    @IBOutlet weak var searchBar: UISearchBar!  
    var searchActive : Bool? 
    var selectedCategoryList = [JSON?]() 
    var filterSelectedCategoryList = [JSON?]() 
    var lab :String? 
    let searchController = UISearchController(searchResultsController: nil) 
    override func viewDidLoad() { 
    super.viewDidLoad() 
      passJson() 

    self.searchBar.delegate = self 
      self.tableView.reloadData() 
    } 

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

    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) 
      tableView.reloadData() 
      // 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 && self.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 && self.searchBar.text != ""{ 

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


       }else{ 
        cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue 
        cell.textLabel?.text = selectedCategoryList[indexPath.row]?["id"].stringValue 
        //cell.textLabel?.text= 
        self.lab = cell.textLabel?.text 

        cell.textLabel?.isHidden = true 
       } 


      } 

      return cell 
     } 
+1

我很高興你的工作,但你爲什麼發佈這個? –

+1

你可以擴大實際問題是什麼?它會幫助回答 –

+0

@Lu的問題,因爲當過濾 – danutha

回答

1

從你的代碼與UISearchBarUISearchController工作,所以你只需要比較方法numberOfRowsInSectioncellForRowAtsearchBar文字,不要和它相比searchController.isActive

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if self.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 self.searchBar.text != ""{ 
     cell.label.text = filterSelectedCategoryList[indexPath.row]?["healerName"].stringValue 

    }else{ 
     cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue 
     cell.textLabel?.text = selectedCategoryList[indexPath.row]?["id"].stringValue 
     //cell.textLabel?.text= 
     self.lab = cell.textLabel?.text 

     cell.textLabel?.isHidden = true 
    } 
    return cell 
} 
+1

當天的救星時,我的數組是空的! – danutha

+0

@danutha歡迎伴侶:) –