如何使用解析JSON文件中的表观视频添加搜索栏
我已经成功地将带有以下数据模型的JSON文件解析到了我的项目和我的tableview中。
import Foundation
struct ActionResult: Codable {
let data: [Datum]
}
struct Datum: Codable {
let goalTitle, goalDescription, goalImage: String
let action: [Action]
}
struct Action: Codable {
let actionID: Int
let actionTit: String
}
现在,我正在尝试创建一个搜索栏来搜索“ ActionTitle”。我的桌面有部分标题和行。
相关代码:
var filteredData: [Action]?
let searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
title = "Search"
searchController.searchBar.delegate = self
filteredData = ????
navigationItem.searchController = searchController
parseJSON()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = []
if searchText == ""{
filteredData = ????
}
else {
for actions in ???? {
if actions.lowercased().contains(searchText.lowercased()) {
filteredData.append(actions)
}
self.tableView.reloadData()
}
我不知道要使用什么代码?
谢谢。
I have successfully parsed a JSON file with the following data model into my project and my tableview.
import Foundation
struct ActionResult: Codable {
let data: [Datum]
}
struct Datum: Codable {
let goalTitle, goalDescription, goalImage: String
let action: [Action]
}
struct Action: Codable {
let actionID: Int
let actionTit: String
}
Now I am trying to create a searchbar to search on the "actionTitle". My tableview has section headers and rows.
Relevant code:
var filteredData: [Action]?
let searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
title = "Search"
searchController.searchBar.delegate = self
filteredData = ????
navigationItem.searchController = searchController
parseJSON()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = []
if searchText == ""{
filteredData = ????
}
else {
for actions in ???? {
if actions.lowercased().contains(searchText.lowercased()) {
filteredData.append(actions)
}
self.tableView.reloadData()
}
I do not know what code to use where I have ????.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保留所有可用数据的实例(
Allactions
),并始终在tableView中显示过滤器数据(filtereddata
)。因此,当没有任何过滤时,filtereddata
等于aLLactions
(,除非您打算在搜索为空时隐藏所有数据)。当
searchbar(_:,textdidchange:)调用
时,您可以使用 filter(_ :) 评估是否应包括该项目。 Apple的描述在过滤器上关闭:我不知道是否存在声明
filtereddata的特定原因:[action]?
,是因为在调用parsejson()
时才填充数据?如果是这样 - 我建议在数据可用时初始化一个空数组并填充它们。另外,
parsedata()
产生datum
的实例吗?我相信您的代码不包括在内,因此我添加了datum:datum?
。如果我错了,请提供更多信息
parsejson()
填充,我将更新答案。You want to keep an instance of all of the available data (
allActions
), and always show your filter data (filteredData
) in the tableView. So when there is nothing to filter,filteredData
is equal toallActions
(unless you intend to hide all data when the search is empty).When
searchBar(_:,textDidChange:)
is called, you can use filter(_:) to evaluate if the item should be included. Apple's description on the filter closure:I don't know if there is a specific reason for declaring
filteredData: [Action]?
, is it because the data is not populated untilparseJSON()
is called? If so--I suggest initializing an empty arrays and populating them when the data is available.Also, does
parseData()
produce an instance ofDatum
? I believe this piece of your code is not included, so I am addingdatum: Datum?
.If I am wrong, please provide more info what
parseJSON()
populates and I will update my answer.