如何过滤模型class中的数据并在table视图中按字母而不是单词准确显示?
How to filter the data in model class and display accurately according to letters not words in table view?
我需要在本地搜索显示在 table 视图中的模型 class 数据,我尝试对成功放置的数据执行搜索,但搜索应该是就像当我输入第一个字母时,它应该根据第一个字母进行过滤,而不是所有对象中的第一个字母
例如,在品牌名称中,我有 Fastrack、Microsoft、Dell、apple、fila
如果我输入 f
那么它应该只显示 fastrack 和 fila 但它显示 fastrack 、fila 和 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({([=12=].brand?.lowercased().contains(lowerCase.lowercased()))!})
}
self.searchActive = true
brandTableView.reloadData()
}
你应该使用 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{[=10=].userName.hasPrefix("T")} // Observe hasPrefix instead of contains
print(array)
为您的代码
self.filteredData = bannerModel.filter({([=11=].brand?.lowercased().hasPrefix(lowerCase.lowercased()))!})
类型安全编辑
self.filteredData = bannerModel.filter({([=12=].brand ?? "").lowercased().hasPrefix("H".lowercased())}
希望对您有所帮助
你可以这样做:
self.filteredData = bannerModel.filter({([=10=].brand?.lowercased().hasPrefix(lowerCase.lowercased()))!})
我需要在本地搜索显示在 table 视图中的模型 class 数据,我尝试对成功放置的数据执行搜索,但搜索应该是就像当我输入第一个字母时,它应该根据第一个字母进行过滤,而不是所有对象中的第一个字母
例如,在品牌名称中,我有 Fastrack、Microsoft、Dell、apple、fila
如果我输入 f
那么它应该只显示 fastrack 和 fila 但它显示 fastrack 、fila 和 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({([=12=].brand?.lowercased().contains(lowerCase.lowercased()))!})
}
self.searchActive = true
brandTableView.reloadData()
}
你应该使用 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{[=10=].userName.hasPrefix("T")} // Observe hasPrefix instead of contains
print(array)
为您的代码
self.filteredData = bannerModel.filter({([=11=].brand?.lowercased().hasPrefix(lowerCase.lowercased()))!})
类型安全编辑
self.filteredData = bannerModel.filter({([=12=].brand ?? "").lowercased().hasPrefix("H".lowercased())}
希望对您有所帮助
你可以这样做:
self.filteredData = bannerModel.filter({([=10=].brand?.lowercased().hasPrefix(lowerCase.lowercased()))!})