I have to perform a search operation in tableview with searchbar.
Which have a label of a person's name and an image for these persons in its cell.
My code is
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
}
The problem is this code only perform a search on the array of label persons. arrPersonImages
is not filtering according to the name of the person entered it the searchbar.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…