如何过滤模型类中的数据并根据字母而不是表格视图中的单词准确显示?

时间:2018-03-19 06:30:15

标签: ios swift uisearchbar

这里我需要对表格视图中显示的模型类数据进行本地搜索,然后我尝试实现搜索成功放置但搜索应该像我输入的第一个字母应该过滤取决于第一个字母,而不是所有对象中的字母

例如在品牌名称中我有 Fastrack,Microsoft,Dell,apple,fila

如果我输入f,则它应仅显示 fastrack fila ,但它会显示 fastrack fila < / strong>和 Microsoft ,其中包含 f

但我需要的是,当第一个字母应该等于f时,它应该显示fastback并且只显示文件而不是Microsoft,如果第二个字母等于任何其他单词,那么它应该显示过滤结果

这是我的代码

 var bannerModel = [BrandBanners]()
 var searchActive : Bool?
 var filteredData = [BrandBanners]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchActive == true {
            return filteredData.count
        }
        else {
            return bannerModel.count
        }
    }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)  as! superBrandTableViewCell
        if searchActive == true {
            cell.brandImageView.kf.indicatorType = .activity
            let item = filteredData[indexPath.row]
            if URL(string: (item.brandImageforMobile)!) != nil {
                let resource = ImageResource(downloadURL: URL(string: (item.brandImageforMobile)!)!, cacheKey: item.brandImageforMobile)
                cell.brandImageView.kf.setImage(with: resource, placeholder: nil, options: nil, progressBlock: nil, completionHandler: nil)
            }
            else {
                cell.brandImageView.image = #imageLiteral(resourceName: "placeholder")
            }
            cell.activityIndicator.stopAnimating()
            self.activityIndicator.stopAnimating()
        }
        else {
            cell.brandImageView.kf.indicatorType = .activity
            let item = bannerModel[indexPath.row]
            if URL(string: (item.brandImageforMobile)!) != nil {
                let resource = ImageResource(downloadURL: URL(string: (item.brandImageforMobile)!)!, cacheKey: item.brandImageforMobile)
                cell.brandImageView.kf.setImage(with: resource, placeholder: nil, options: nil, progressBlock: nil, completionHandler: nil)
            }
            else {
                cell.brandImageView.image = #imageLiteral(resourceName: "placeholder")
            }
            cell.activityIndicator.stopAnimating()
            self.activityIndicator.stopAnimating()
        }
        return cell
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchBar.text == nil || searchBar.text == "" {
            self.filteredData = self.bannerModel
        } else {
            var lowerCase = searchBar.text!
            self.filteredData = bannerModel.filter({($0.brand?.lowercased().contains(lowerCase.lowercased()))!})
        }
        self.searchActive = true
        brandTableView.reloadData()
    }

2 个答案:

答案 0 :(得分:0)

您应该使用hasPrefix方法来实现此目标

示例

struct User {
    var userName : String = ""
    var password : String = ""
}



var array = [User.init(userName:"Test",password:"123456"),User.init(userName:"TEMP",password:"123456"),User.init(userName:"Prashant",password:"123456"),User.init(userName:"Test",password:"123456")]

array = array.filter{$0.userName.hasPrefix("T")} // Observe hasPrefix instead of contains  
print(array)

代码

self.filteredData = bannerModel.filter({($0.brand?.lowercased().hasPrefix(lowerCase.lowercased()))!})

编辑类型安全

    self.filteredData = bannerModel.filter({($0.brand ?? "").lowercased().hasPrefix("H".lowercased())}

希望它有用

答案 1 :(得分:0)

你可以这样做:

self.filteredData = bannerModel.filter({($0.brand?.lowercased().hasPrefix(lowerCase.lowercased()))!})