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
완벽하게 작동합니다. 프로젝트가 커지면이 작은 세부 사항을 놓치기는 꽤 정상입니다. 감사합니다. – Christian
이 상황이 발생하지 않도록하는 안전한 습관으로, 보통 첫 번째 줄에서 셀을 대기열에서 제외하고 마지막으로 반환합니다. 실수 내가 세포가 엉망이라면 틀린 내용으로 세포가 돌아올 것입니다. 어떤 이유로 든 빈 셀을 반환 할 필요가 있다면 아무것도 반환하지 않고 return nil을 호출하여 아무 것도 반환하지 않는다는 것을 분명히합니다. –