数组类型为 JSON 时如何实现自定义搜索栏
Posted
技术标签:
【中文标题】数组类型为 JSON 时如何实现自定义搜索栏【英文标题】:How to implement a custom search-bar when the array type is JSON 【发布时间】:2017-03-16 09:03:51 【问题描述】:我正在 swift 3 中开发一个项目,我有一个特定的 UIViewController,我有一个 UITableView 并且要在其单元格上填充数据,数据是从服务器获取的,我将它分配给类型的数组JSON。因此,我在同一个 UIViewController 的不同位置放置了一个 UISearch 栏(而不是作为 UITableView 的标题)。我的要求是实现搜索栏的功能,我可以在其中过滤和搜索 UITableView 的数据。我怎样才能做到这一点?我在代码中工作了一半,但它没有功能,代码如下。
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
【问题讨论】:
你的代码有什么问题?你想过滤什么? 不知道它不起作用。单击搜索栏后程序崩溃 @danutha 你能显示崩溃位置的屏幕截图吗? @danutha 检查我的解决方案一次以过滤您的阵列。 【参考方案1】:尝试实现UISearchBarDelegate
方法,但在此之前将searchBar.delegate
设置为self
在viewDidLoad
中。
override func viewDidLoad()
super.viewDidLoad()
passJson()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.sizeToFit()
//Set the delegate of searchBar
searchController.searchBar.delegate = self
self.tableView.reloadData()
现在实现UISearchBarDelegate
的searchBar(_:textDidChange:)
方法并在该方法中过滤您的数组。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
filterSelectedCategoryList = selectedCategoryList.filter titles in
return (titles?["healerName"].stringValue.lowercased().contains(searchText.lowercased()))!
tableView.reloadData()
【讨论】:
我删除了“filterContentFroSearchedText”方法和“updateSearchResults”,然后用函数“searchBar”替换。虽然这次我的代码没有崩溃,但它没有提供搜索功能 @danutha 使用您正在尝试的新代码编辑您的问题。 @danutha 在passJson
行self.selectedCategoryList = selectedItemArray
之后重新加载tableView
并检查您的tableView 是否填充了视图数据?
让我们continue this discussion in chat。以上是关于数组类型为 JSON 时如何实现自定义搜索栏的主要内容,如果未能解决你的问题,请参考以下文章
iOS7标签栏自定义图标高度 - 高度减小直到图标变得不可见