신속한 3에서 프로젝트를 진행하고 있으며 UITableView가 있으며 해당 셀의 데이터를 채우는 특정 UIViewController가 있습니다. 서버에서 가져와 JSON 유형의 배열에 할당합니다. 따라서 UITearchView의 머리글이 아닌 동일한 UIViewController의 다른 위치에 배치 된 UISearch 막대가 있습니다. 내 요구 사항은 필터 및 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
}
}
코드에서 무엇이 잘못 되었습니까? 무엇을 필터링하고 싶습니까? – Poles
그다지 잘 모른다. 검색 창을 클릭하면 프로그램이 충돌합니다. – danutha
@danutha 충돌이 발생한 곳의 스크린 샷을 표시 할 수 있습니까? –