我无法使用 swift 中的搜索栏 search/filter 图片
I am not able to search/filter images using a search bar in swift
我在顶部有一个 tableView 和一个搜索栏,自定义单元格有一个 ImageView 和一个标签,文本是从字符串数组中提取的,图片是从 UIImage 数组中提取的,但是当我尝试过滤条目时,虽然我已经用字符串实现了这一点,我不能用搜索时得到的 images.What 是相关的字符串条目(例如三个以 'c' 开头的条目)但是过滤后的图像条目是例如,UIImage 数组中的前三个图像。
import UIKit
let firstArray = [ "APC","Apple","Autonomic","Axis","B&W","BSS","Central","Sisco","Crestron","Crown","Denon","Digital","Direct Tv"]
class FirstTableViewController: UITableViewController, UISearchBarDelegate {
//for search bar
@IBOutlet weak var searchBar: UISearchBar!
var filteredData: [String]!
var filteredImages: [UIImage]!
var searching = false
var firstArray = [String]()
var images = [UIImage(named:"APC"),UIImage(named:"Apple"),UIImage(named:"Autonomic"),UIImage(named:"Axis"),UIImage(named:"B&W"),UIImage(named:"BSS"),
UIImage(named:"Central"),UIImage(named:"Sisco"),UIImage(named:"Crestron"),UIImage(named:"Crown"),UIImage(named:"Denon"),
UIImage(named:"Digital"),UIImage(named:"Direct Tv")]
override func viewDidLoad() {
super.viewDidLoad()
//for search bar
searchBar.delegate = self
filteredData = firstArray
firstArray = [ "APC","Apple","Autonomic","Axis","B&W","BSS","Central","Cisco","Crestron","Crown","Denon","Digital","Direct Tv"]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searching
{
return filteredData.count
}else{
return firstArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableTableViewCell
Cell.textLabel?.textColor = .systemBlue
Cell.textLabel?.font = .boldSystemFont(ofSize: 20)
if searching
{
Cell.textLabel?.text = filteredData[indexPath.row]
Cell.imageManuf?.image = self.images[indexPath.row]
}else{
Cell.textLabel?.text = firstArray[indexPath.row]
Cell.imageManuf?.image = self.images[indexPath.row]
}
return Cell
}
//Mark: Search Bar config
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = firstArray.filter({[=12=].lowercased().prefix(searchText.count) == searchText.lowercased()})
searching = true
//to reload the data
self.tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
tableView.reloadData()
}
}
你能帮忙吗?我对图像有各种各样的问题,我似乎没有掌握它们..
您应该创建一个结构,以便每个字符串都与相应的图像“分组”。每个“string-image”对代表某种东西,对吧?
struct ImageWithName { // please think of a better name than this
let image: UIImage
let name: String
init(name: String) {
self.name = name
image = UIImage(named: name)!
}
}
与其存储两个数组,一个用于字符串,一个用于图像,您可以只存储一个 ImageWithName
数组:
var filteredData: [ImageWithName]!
var searching = false
var firstArray = [ImageWithName]()
override func viewDidLoad() {
super.viewDidLoad()
//for search bar
searchBar.delegate = self
firstArray = ["APC","Apple","Autonomic","Axis","B&W","BSS","Central","Cisco","Crestron","Crown","Denon","Digital","Direct Tv"]
.map(ImageWithName.init(name:))
filteredData = firstArray
}
在 cellForRowAt
中,访问 .name
和 .image
:
if searching
{
cell.textLabel?.text = filteredData[indexPath.row].name
cell.imageManuf?.image = filteredData[indexPath.row].image
}else{
cell.textLabel?.text = firstArray[indexPath.row].name
cell.imageManuf?.image = firstArray[indexPath.row].image
}
在textDidChange
中,访问.name
搜索姓名:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = firstArray.filter({[=13=].name.lowercased().prefix(searchText.count) == searchText.lowercased()})
我在顶部有一个 tableView 和一个搜索栏,自定义单元格有一个 ImageView 和一个标签,文本是从字符串数组中提取的,图片是从 UIImage 数组中提取的,但是当我尝试过滤条目时,虽然我已经用字符串实现了这一点,我不能用搜索时得到的 images.What 是相关的字符串条目(例如三个以 'c' 开头的条目)但是过滤后的图像条目是例如,UIImage 数组中的前三个图像。
import UIKit
let firstArray = [ "APC","Apple","Autonomic","Axis","B&W","BSS","Central","Sisco","Crestron","Crown","Denon","Digital","Direct Tv"]
class FirstTableViewController: UITableViewController, UISearchBarDelegate {
//for search bar
@IBOutlet weak var searchBar: UISearchBar!
var filteredData: [String]!
var filteredImages: [UIImage]!
var searching = false
var firstArray = [String]()
var images = [UIImage(named:"APC"),UIImage(named:"Apple"),UIImage(named:"Autonomic"),UIImage(named:"Axis"),UIImage(named:"B&W"),UIImage(named:"BSS"),
UIImage(named:"Central"),UIImage(named:"Sisco"),UIImage(named:"Crestron"),UIImage(named:"Crown"),UIImage(named:"Denon"),
UIImage(named:"Digital"),UIImage(named:"Direct Tv")]
override func viewDidLoad() {
super.viewDidLoad()
//for search bar
searchBar.delegate = self
filteredData = firstArray
firstArray = [ "APC","Apple","Autonomic","Axis","B&W","BSS","Central","Cisco","Crestron","Crown","Denon","Digital","Direct Tv"]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searching
{
return filteredData.count
}else{
return firstArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableTableViewCell
Cell.textLabel?.textColor = .systemBlue
Cell.textLabel?.font = .boldSystemFont(ofSize: 20)
if searching
{
Cell.textLabel?.text = filteredData[indexPath.row]
Cell.imageManuf?.image = self.images[indexPath.row]
}else{
Cell.textLabel?.text = firstArray[indexPath.row]
Cell.imageManuf?.image = self.images[indexPath.row]
}
return Cell
}
//Mark: Search Bar config
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = firstArray.filter({[=12=].lowercased().prefix(searchText.count) == searchText.lowercased()})
searching = true
//to reload the data
self.tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
tableView.reloadData()
}
}
你能帮忙吗?我对图像有各种各样的问题,我似乎没有掌握它们..
您应该创建一个结构,以便每个字符串都与相应的图像“分组”。每个“string-image”对代表某种东西,对吧?
struct ImageWithName { // please think of a better name than this
let image: UIImage
let name: String
init(name: String) {
self.name = name
image = UIImage(named: name)!
}
}
与其存储两个数组,一个用于字符串,一个用于图像,您可以只存储一个 ImageWithName
数组:
var filteredData: [ImageWithName]!
var searching = false
var firstArray = [ImageWithName]()
override func viewDidLoad() {
super.viewDidLoad()
//for search bar
searchBar.delegate = self
firstArray = ["APC","Apple","Autonomic","Axis","B&W","BSS","Central","Cisco","Crestron","Crown","Denon","Digital","Direct Tv"]
.map(ImageWithName.init(name:))
filteredData = firstArray
}
在 cellForRowAt
中,访问 .name
和 .image
:
if searching
{
cell.textLabel?.text = filteredData[indexPath.row].name
cell.imageManuf?.image = filteredData[indexPath.row].image
}else{
cell.textLabel?.text = firstArray[indexPath.row].name
cell.imageManuf?.image = firstArray[indexPath.row].image
}
在textDidChange
中,访问.name
搜索姓名:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = firstArray.filter({[=13=].name.lowercased().prefix(searchText.count) == searchText.lowercased()})