如何解决如何在tableview中过滤部分明智的行?
我是 iOS 新手,正在创建基于 JSON 响应的演示应用程序。
我的查询是我不知道如何明智地编写和过滤行数据部分以在 tableview 上显示。
category.json [文件]
{
"category": [
{
"vName": "Section 0","skill": [
{
"vName": "Section 0 Row 1",},{
"vName": "Section 0 Row 2",}
]
},{
"vName": "Section 1","skill": [
{
"vName": "Section 1 Row 1",{
"vName": "Section 1 Row 2",{
"vName": "Section 2","skill": [
{
"vName": "Section 2 Row 1",{
"vName": "Section 2 Row 2",{
"vName": "Section 3","skill": [
{
"vName": "Section 3 Row 1",{
"vName": "Section 3 Row 2",}
]
}
]
}
Response.Swift [文件]
//
// Response.swift
// SectionTableSearchDemo
//
import Foundation
struct Welcome: Codable {
let category: [Category]
}
struct Category: Codable {
let vName: String
let skill: [Skill]
enum CodingKeys: String,CodingKey {
case vName,skill
}
}
struct Skill: Codable {
let vName: String
enum CodingKeys: String,CodingKey {
case vName
}
}
#SectionTableviewController.swift [文件]
//
// SectionTableViewController.swift
// SectionTableSearchDemo
//
import UIKit
class SectionTableViewController: UITableViewController,UISearchResultsUpdating {
var cat : Welcome?
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
searchControllerSetup()
}
private func searchControllerSetup(){
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
// i don't kNow how to implement and show only skills when search
func updateSearchResults(for searchController: UISearchController) {
}
private func parseJSON(){
guard let path = Bundle.main.path(forResource: "category",ofType: "json")else{
return
}
let url = URL(fileURLWithPath: path)
do {
let jsonData = try Data(contentsOf: url)
cat = try JSONDecoder().decode(Welcome.self,from: jsonData)
if let result = cat {
print(result)
}
} catch {
print("Error is \(error)")
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation,return the number of sections
return (cat?.category.count)!
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation,return the number of rows
if let cat = cat {
return cat.category[section].skill.count
}else{
return 0
}
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
let text = cat?.category[indexPath.section].skill[indexPath.row].vName
cell.textLabel?.text = text
return cell
}
override func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
let titleText = cat?.category[section].vName
return titleText
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView,canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath],with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class,insert it into the array,and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView,moveRowAt fromIndexPath: IndexPath,to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView,canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application,you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
以及我对what I did
的形象请帮我弄清楚
谢谢
解决方法
要在模型内搜索数据,您可以使用过滤器功能。 与您在 tableview 单元格中显示数据的方式相同。您可以使用过滤器功能进行搜索,并根据搜索标志显示其中的数据。
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
guard let searchText = searchText,!searchText.isEmpty else {
return
}
searchData = cat?.category.map({
$0.skill.filter({$0.vName == searchText})
}).filter({ $0.count > 0 })
searching = true
self.searchedSkillsResults = searchData
tableView.reloadData()
}
//在这里检查您的搜索是否为真,并显示技能数组,否则搜索为假的原始数组结果。
参考链接:how to search data from model in swift?
//下面的链接可帮助您在嵌套数组中进行搜索。 Swift Filter Nested Array
我正在运行的示例代码:
import Foundation
struct Welcome: Codable {
let category: [Category]
}
struct Category: Codable {
let vName: String
let skill: [Skill]
enum CodingKeys: String,CodingKey {
case vName,skill
}
}
struct Skill: Codable {
let vName: String
enum CodingKeys: String,CodingKey {
case vName
}
}
var cat : Welcome?
private func parseJSON(){
guard let path = Bundle.main.path(forResource: "category",ofType:
"json")else{
return
}
let url = URL(fileURLWithPath: path)
print(url)
do {
let jsonData = try Data(contentsOf: url)
cat = try JSONDecoder().decode(Welcome.self,from: jsonData)
if let result = cat {
print(result)
}
} catch {
print("Error is \(error)")
}
}
parseJSON()
//print(cat)
let searchText = "Section 0 Row 1"
let result = cat?.category.map({
$0.skill.filter({$0.vName.lowercased().contains(searchText.lowercased())})
}).filter({ $0.count > 0 })
//output = Optional([[Skill(vName: "Section 0 Row 1")]])
,
尝试如下。
添加如下两个变量。
var filteredProducts = [Category]()
var products: [Category] = []
并初始化产品变量
///......
let jsonData = try Data(contentsOf: url)
cat = try JSONDecoder().decode(Welcome.self,from: jsonData)
products = cat.category // add this line
现在修改表视图数据源如下
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation,return the number of sections
if searchController.isActive && searchController.searchBar.text != "" {
return filteredProducts.count
}
return products.count
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation,return the number of rows
if searchController.isActive && searchController.searchBar.text != "" {
return filteredProducts[section].skill.count
}else{
return products[section].skill.count
}
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
let product: Category
if searchController.isActive && searchController.searchBar.text != "" {
product = filteredProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
let text = product.skill[indexPath.row].vName
cell.textLabel?.text = text
return cell
}
override func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
let product: Category
if searchController.isActive && searchController.searchBar.text != "" {
product = filteredProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
let titleText = product[section].vName
return titleText
}
然后创建一个函数来过滤你的数据
func filterContentForSearchText(_ searchText: String) {
filteredProducts = products.filter({( product : Category) -> Bool in
return product.title!.lowercased().contains(searchText.lowercased())
})
yourTableView.reloadData() // reload your table view
}
调用filterContentForSearchText()
func searchBar(_ searchBar: UISearchBar,selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text!)
}
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。