在类阵列中的过滤结果(swift 3)



Class

class Objects {
    var number: Int!
    var name: String!
    init(number: Int, name: String) {
        self.number = number
        self.name = name
    }
}        

viewController

    var allObjects = [Objects]()
    var inSearchMode = false
    @IBOutlet weak var searchBar: UISearchBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.delegate = self
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell

        if inSearchMode {
            let fill: Objects!
            fill = filteredObject[indexPath.row]
            cell.configureCell(fill)
        } else {
            let fill: Objects!
            fill = allObjects[indexPath.row]
            cell.configureCell(fill)
            return cell
        }
    }
    return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if inSearchMode {
            return filteredObject.count
        }
        return allObjects.count
        }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text == nil || searchBar.text == "" {
            inSearchMode = false
            tableView.reloadData()
            view.endEditing(true)
        } else {
            inSearchMode = true
            var lowerCase = Int(searchBar.text!)
            filteredObject = allObjects.filter({$0.number == lowerCase})
            tableView.reloadData()
            print(filteredObject)
        }
    }

我想拥有一个过滤的搜索栏,并且仅显示一个结果,其中包含我们正在寻找的数字。我考虑过使用contains,并将我们从搜索栏输入的数字。

我设法将一个对象放入filteredObject,但不会显示在tableView

仔细查看代码的这一部分:

if inSearchMode {
    let fill: Objects!
    fill = filteredObject[indexPath.row]
    cell.configureCell(fill)
} else {
    let fill: Objects!
    fill = allObjects[indexPath.row]
    cell.configureCell(fill)
    return cell
}

当您处于搜索模式时,您没有返回单元格,这就是为什么您在搜索时看不到任何东西的原因。

我将使用计算的属性来驱动tableview;此属性是所有对象或过滤对象:

var allObjects = [Objects]()
var filteredObjects: [Objects]?
var objects: [Objects] = {
    return filteredObjects ?? allObjects
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return self.objects.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
    let fill = self.objects[indexPath.row]
    cell.configureCell(fill)
    return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {
        self.filteredObjects = nil
    } else {
        var lowerCase = Int(searchBar.text!)
        self.filteredObjects = allObjects.filter({$0.number == lowerCase})
    }
    tableView.reloadData()
}

最新更新