2017-03-20 2 views
0

신속한 3에서 프로젝트를 진행하고 있으며 UITableView가 있고 해당 셀의 데이터를 채우는 특정 UIViewController가 있으며 데이터는 서버에서 가져와 할당됩니다. 그것은 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

당신은 UISearchBar하지 UISearchController와 함께 노력하고 있습니다, 그래서 당신은 그것으로 searchController.isActive을 비교하지 않는다, 단지 방법 numberOfRowsInSectioncellForRowAt에서의 searchBar 텍스트를 비교해야합니다.

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

@ 다누 타 환영 메이트 :) –