首页 > 解决方案 > 使用 UIExfield 搜索

问题描述

我想问一下。我如何搜索从对象映射的字符串数组而不是从 uitextfield 搜索它,我尝试了这种方法,但它没有用。我哪里做错了?这是我的代码设置。

var cities = [City]()
var cityNames = [String]()

    private func populateCities() {
        BasicInfoServices.shared.getCity { [weak self] result in
            switch result {
            case .success(let cities):
                self?.cities = cities
                let names = cities.compactMap { $0.name }
                self?.cityNames = names
            case .failure(let error):
                print(error)
            }
        }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CitiesAlertCell.cellId, for: indexPath) as! CitiesAlertCell
        cell.selectionStyle = .none
        let item = cityNames[indexPath.row]
        cell.item = item
        return cell
    }

    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        searchTextField.resignFirstResponder()
        cityNames.removeAll()
        return true
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if searchTextField.text?.count != nil {
            self.cityNames.removeAll()
            for str in cityNames {
                let range = str.lowercased().range(of: textField.text!, options: .caseInsensitive, range: nil, locale: nil)
                if range != nil {
                    cityNames.append(str)
                }
            }
        }
        tableView.reloadData()
        return true
    }

标签: iosswiftuitextfield

解决方案


所以,去除杂乱,你基本上是在做这个......

self.cityNames.removeAll()
for str in cityNames {
    //...
    cityNames.append(str)
}

您从数组中删除所有元素并尝试对其进行迭代,但是由于现在数组中没有任何内容,因此没有可以迭代的内容。

您可以使用“过滤”数组,它从“主”数组中获取输入。“过滤”数组是向用户显示的内容

var filteredCities: [String]()

private func populateCities() {
    BasicInfoServices.shared.getCity { [weak self] result in
        switch result {
        case .success(let cities):
            self?.cities = cities
            let names = cities.compactMap { $0.name }
            self?.cityNames = names
            self?.filteredCities = names
        case .failure(let error):
            print(error)
        }
    }
}

然后,当您需要过滤列表时...

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if searchTextField.text?.count != nil {
        self.filteredCities.removeAll()
        for str in cityNames {
            let range = str.lowercased().range(of: textField.text!, options: .caseInsensitive, range: nil, locale: nil)
            if range != nil {
                filteredCities.append(str)
            }
        }
    }
    tableView.reloadData()
    return true
}

这当然意味着您将需要使用filteredCities而不是cityNames在您的表数据源中;)

说了这么多。我会考虑做一些稍微不同的事情。而不是使用主/过滤器列表,只需将cities数组用作主并直接过滤它......

if let text = searchTextField.text, !text.isEmpty else { 
    cityNames = cities.filter { $0.name.lowercased().range(of: text, options: .caseInsensitive, range: nil, locale: nil) != nil }.map { $0.name }
} else {
    cityNames = cities.map { $0.name }
}

如果速度是一个问题,您可以回退到主/过滤String值列表,这将允许您消除对map结果的需要,但在这一点上,我会质疑为什么您仍然需要一个String列表

例如...

var cities = [City]() // Master
var cityFilter = [City]() // Filter

private func populateCities() {
    BasicInfoServices.shared.getCity { [weak self] result in
        switch result {
        case .success(let cities):
            self?.cities = cities
            self?.cityFilter = cities
        case .failure(let error):
            print(error)
        }
    }
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: CitiesAlertCell.cellId, for: indexPath) as! CitiesAlertCell
    cell.selectionStyle = .none
    let item = cityFilter[indexPath.row]
    cell.item = item // The item is now a city reference
    return cell
}

func textFieldShouldClear(_ textField: UITextField) -> Bool {
    searchTextField.resignFirstResponder()
    cityFilter = cities
    return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if let text = searchTextField.text, !text.isEmpty else { 
        cityFilter = cities.filter { $0.name.lowercased().range(of: text, options: .caseInsensitive, range: nil, locale: nil) != nil }
    } else {
        cityFilter = cities
    }
    tableView.reloadData()
    return true
}

如果您担心城市nil名称,那么我会filter使用原始列表,以便主列表仅包含有效值


推荐阅读