gpt4 book ai didi

swift - 当搜索栏为空或为零时表为空

转载 作者:行者123 更新时间:2023-11-28 15:23:13 26 4
gpt4 key购买 nike

当我搜索商店时,表格找到了正确的商店。但是当我编辑我的搜索词时(例如删除一个字母),表格就变空了。即使我删除了搜索词,表格也变空了!我必须转到另一个 View Controller,然后返回到搜索栏才能看到完整的表格。

这是我的代码

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

shops = shops.filter { $0.shopname.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil}


tableView.reloadData()

}

如果 searchBar.text == ""|| 我应该在哪里实现 searchBar.text ==nill 返回原始表格单元格。或者再次搜索时我希望它执行另一次搜索。

更新 enter image description here

我的商店数组不是字符串类型,它是一个包含字符串的类类型。因为它是一个带有来自 api 的 JSON 数据的自定义单元格

 var shops: [Shops]!
var isSearch = false
var auxiliar : [String] = []
var searchActive: Bool = false



class Shops {
private var _familiy_id: String?
private var _logo : String?

private var _shopname : String?

var familiy_id : String{
return _familiy_id!
}

var shopname : String{
return _shopname!
}
var Logo : String{
return _logo!
}
init(shopname : String , Logo : String , family_id : String) {

self._shopname = shopname
self._logo = Logo
self._familiy_id = family_id
}

最佳答案

问题出在这一行:

shops = shops.filter {  ... }

由于您只是应用过滤器并与原始数组重叠,因此您将丢失元素。需要一个辅助阵列来帮助保持原始状态。

一个简单的例子:(代码更新)

import UIKit

class Shops {
private var _familiy_id: String?
private var _logo : String?
private var _shopname : String?

var familiy_id : String{
return _familiy_id!
}

var shopname : String{
return _shopname!
}
var Logo : String{
return _logo!
}
init(shopname : String , Logo : String , family_id : String) {
self._shopname = shopname
self._logo = Logo
self._familiy_id = family_id
}
}



class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!

var shops : [Shops]! = []
var auxiliar : [Shops]!

override func viewDidLoad() {
super.viewDidLoad()
// 1 - load data to shops array
shops.append(Shops(shopname: "Brasil", Logo: "BR", family_id: "1"))
shops.append(Shops(shopname: "Brasolia", Logo: "BA", family_id: "2"))
shops.append(Shops(shopname: "Colombia", Logo: "CO", family_id: "3"))
shops.append(Shops(shopname: "Argentina", Logo: "AR", family_id: "4"))




// 2 - auxiliar receive the complete original array
auxiliar = shops

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return auxiliar.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = auxiliar[indexPath.row].shopname
return cell
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
auxiliar = shops.filter { $0.shopname.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil }
if searchText == "" {
// 3 if there is nothing to search, auxiliar receive the complete orihinal array
auxiliar = shops
}

tableView.reloadData()
}
}

关于swift - 当搜索栏为空或为零时表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624226/

26 4 0