我必须使用搜索栏在tableview中执行搜索操作 其中有一个人名的标签和这些人在其牢房中的图像。
我的代码是
override func viewDidLoad() {
super.viewDidLoad()
ArrPersons = ["Mahatma Gandhi","Pramukh Swami","Akshay Kumar","Sachin Tendulkar","Chetan Bhagat","Sardar Vallabhai Patel","Amitabh Bachchan"]
arrPersonImages = ["1.png","2.png","3.png","4.png","5.png","6.png","7.png"]
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if (searchText.characters.count>0) {
let predicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)
ArrPersons = arrTemp
let array = (self.ArrPersons as NSArray).filteredArrayUsingPredicate(predicate)
print(array)
ArrPersons = array as! [String]
}
else
{
ArrPersons = arrTemp
}
self.tableviewww.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.ArrPersons.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableviewww.dequeueReusableCellWithIdentifier("mycell")as! buildVcCell
cell.personsImages.image = UIImage (named:arrPersonImages[indexPath.row] )
cell.labelPersonNamess?.text = self.ArrPersons[indexPath.row]
cell.addBtn.addTarget(self, action: #selector(BuildVc.AddbuttonClicked(_:)), forControlEvents: .TouchUpInside)
return cell
}
问题是此代码仅对标签人员阵列执行搜索。 arrPersonImages
未根据在搜索栏中输入的人的姓名进行过滤。
答案 0 :(得分:2)
您应该为Person创建一个“模型”(使用MVC模式):
首先,创建“人物”模型:
struct Person {
var name: String?
var imageName: String?
}
不是使用两个独立的数组来存储人员的数据,而是可以创建一个Person Model数组:
// add those vars to your ViewController:
var persons = [Person]()
var filteredPersons = [Person]()
var isFiltering = false
override func viewDidLoad() {
super.viewDidLoad()
persons = [Person(name: "Ahmad", imageName: "img.png"), Person(name: "Harry", imageName: "img.png")]
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if (searchText.characters.count>0) {
isFiltering = true
filteredPersons = persons.filter {
$0.name?.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
}
print(filteredPersons)
}
else
{
isFiltering = false
filteredPersons = persons
}
self.tableviewww.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return isFiltering == true ? filteredPersons.count : persons.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//...
// getting the current person
let currentPerson = isFiltering == true ? filteredPersons[indexPath.row] : persons[indexPath.row]
// do the rest of the implementation...
//...
}
注意这是Swift 3 Code。
答案 1 :(得分:1)
我认为将每个字符的名称和图像组合到模型结构中会更有意义,而是将其用于单元格的基础。话虽这么说,以下内容可以帮助您在不改变现有代码的情况下。它也会回避实际改变你的数组...
一个很好的动态变量,可以让其他地方更加自动化:
var filteredPersons: [String] {
return arrPersons.filter{ $0.contains(searchString) }
}
哪个会给你
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredPersons.count
}
然后在你的cellForRowAtIndexPath函数中,您可以创建如下名称和图像:
let name = filteredPersons[indexPath.row]
let imageIndex = arrPersons.index(of: name)
let image = UIImage(named: arrPersonImages[imageIndex])
答案 2 :(得分:0)
进行搜索的最佳方法是使用包含字典属性的数组来保留搜索结果。仅供参考:
class PersonController: UITableViewController {
let ArrPersons = [["name": "Mahatma Gandhi", "image": "1.png"], ["name": "Akshay Kumar", "image": "2.png"]]
var searchResult: [[String: String]]!
override func viewDidLoad() {
super.viewDidLoad()
searchResult = ArrPersons
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if (searchText.characters.count>0) {
searchResult = arr.filter { (item) -> Bool in
let name = item["name"]
return name!.containsString(searchText)
}
}
else
{
searchResult = ArrPersons
}
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.searchResult.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("mycell")as! buildVcCell
let person = searchResult[indexPath.row]
cell.personsImages.image = UIImage(named: person["image"]) // Person image
cell.labelPersonNamess?.text = person["name"] // Person name
cell.addBtn.addTarget(self, action: #selector(BuildVc.AddbuttonClicked(_:)), forControlEvents: .TouchUpInside)
return cell
}
}