搜索栏为空或为零时表为空 [英] Table is empty when search bar is empty or nil

查看:32
本文介绍了搜索栏为空或为零时表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我搜索商店时,表格会显示正确的商店.但是当我编辑我的搜索词(例如删除一个字母)时,表格变空了.即使我删除了搜索词,表格也变空了!我必须转到不同的视图控制器,然后返回搜索栏才能查看完整的表格.

When I search for a shop the table gets the right shop. but when I edit my search words ( delete one letter for example ) the table becomes empty. Even when I delete the search words the table becomes empty! I have to go to a different View Controller and come back to the search bar to see the full table.

这是我的代码

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

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


    tableView.reloadData()

}

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

Where should I implement if searchBar.text == "" || searchBar.text ==nill return the original table cells. or when search again I want it to perform another search.

更新

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

my shops array isn't a string type it's a class type that has strings within it. because it's a custom cell with JSON data from api

 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 {  ... }

由于您仅应用过滤器并与原始数组重叠,因此您将丢失元素.需要一个辅助数组来帮助保持原始.

As you are only applying the filter and overlapping the original array then you will lose the elements. An auxiliary array is needed that helps keep the original.

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

A simple example: (code updated)

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()
    }
}

这篇关于搜索栏为空或为零时表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆