2016-12-14 1 views
1

필터 결과 (들) (스위프트 3)

class Objects { 

    var number: Int! 
    var name: String! 

    init(number: Int, name: String) { 
     self.number = number 
     self.name = name 
    } 
}   

viewController

var allObjects = [Objects]() 
    var inSearchMode = false 

    @IBOutlet weak var searchBar: UISearchBar! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     searchBar.delegate = self 

    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 


     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell 


     if inSearchMode { 

      let fill: Objects! 
      fill = filteredObject[indexPath.row] 
      cell.configureCell(fill) 

     } else { 

      let fill: Objects! 
      fill = allObjects[indexPath.row] 
      cell.configureCell(fill) 

      return cell 
     } 

    } 

    return UITableViewCell() 

    } 

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

     if inSearchMode { 

      return filteredObject.count 

     } 

     return allObjects.count 

     } 

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

     return 1 

    } 

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

     if searchBar.text == nil || searchBar.text == "" { 

      inSearchMode = false 
      tableView.reloadData() 
      view.endEditing(true) 

     } else { 

      inSearchMode = true 
      var lowerCase = Int(searchBar.text!) 
      filteredObject = allObjects.filter({$0.number == lowerCase}) 
      tableView.reloadData() 

      print(filteredObject) 

     } 

    } 

에서 Class에서 나는 필터는 하나 개의 결과를 보여주는 검색 창을 가지고 싶습니다 우리가 찾고있는 번호가 들어 있습니다. contains을 사용하고 검색 창에서 입력 한 숫자를 입력하는 방법에 대해 생각했습니다.

은 내가 filteredObject에 하나 개의 객체를 얻기 위해 관리하지만,에 표시되지 않습니다 tableView

답변

3

봐 :이 속성은 모든 개체 또는 필터링 된 개체 (들) 중 하나입니다 검색 할 때 아무 것도 볼 수없는 이유입니다.

+0

완벽하게 작동합니다. 프로젝트가 커지면이 작은 세부 사항을 놓치기는 꽤 정상입니다. 감사합니다. – Christian

+0

이 상황이 발생하지 않도록하는 안전한 습관으로, 보통 첫 번째 줄에서 셀을 대기열에서 제외하고 마지막으로 반환합니다. 실수 내가 세포가 엉망이라면 틀린 내용으로 세포가 돌아올 것입니다. 어떤 이유로 든 빈 셀을 반환 할 필요가 있다면 아무것도 반환하지 않고 return nil을 호출하여 아무 것도 반환하지 않는다는 것을 분명히합니다. –

2

내가있는 tableview를 구동하기 위해 계산 된 속성을 사용합니다; 당신이 검색 모드에있을 때 당신은 셀을 반환하지 않은

if inSearchMode { 

    let fill: Objects! 
    fill = filteredObject[indexPath.row] 
    cell.configureCell(fill) 

} else { 

    let fill: Objects! 
    fill = allObjects[indexPath.row] 
    cell.configureCell(fill) 

    return cell 
} 

: 코드의이 부분을주의 깊게

var allObjects = [Objects]() 
var filteredObjects: [Objects]? 

var objects: [Objects] = { 
    return filteredObjects ?? allObjects 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.objects.count 
} 


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

    let fill = self.objects[indexPath.row] 
    cell.configureCell(fill) 

    return cell 
} 

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

    if searchBar.text == nil || searchBar.text == "" { 
     self.filteredObjects = nil 
    } else { 
     var lowerCase = Int(searchBar.text!) 
     self.filteredObjects = allObjects.filter({$0.number == lowerCase}) 
    } 
    tableView.reloadData() 
}