我创建了一个包含标签和图像的单元格的TableView。当我不使用搜索栏时,一切正常,但是当我使用搜索栏图片时,1.png总是首先显示。这是使用搜索栏将图片连接到标签的方法吗?谢谢你的帮助
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
var TitleList = ["101", "121", "122", "131", "132"]
let ImageList = ["1.png", "2.png", "3.png", "4.png", "5.png"]
var filteredfarger = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.resultSearchController.active {
return self.filteredfarger.count
}
else {
return self.TitleList.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
cell.LabelTitle.text = TitleList[indexPath.row]
cell.CellDescription.text = DescriptionList[indexPath.row]
let imagename = UIImage(named: ImageList[indexPath.row])
cell.CellImage.image = imagename
if self.resultSearchController.active {
cell.LabelTitle?.text = self.filteredfarger[indexPath.row]
}
else {
cell.LabelTitle?.text = self.TitleList[indexPath.row]
}
return cell
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredfarger.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.TitleList as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredfarger = array as! [String]
self.tableView.reloadData()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailedViewController
if let cell = sender as? TableViewCell {
VC.SentData1 = cell.LabelTitle.text
}
}
}
答案 0 :(得分:1)
搜索时,过滤后的数组将只有匹配的标题。最有可能只有一个匹配,所以filteredfarger.count为1.
在搜索时调用cellForRowAtIndexPath时,将根据UIImage(named: ImageList[indexPath.row])
生成图像。由于filteredfarger.count为1,因此只会创建1行,同样indexPath.row
将为0.这将导致始终从ImageList数组中引用图像1.png。
答案 1 :(得分:0)
以下是使用字典的代码示例。我还没有使用Xcode检查或编译过这个,但是你可以看到一般的想法。
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
// This is an array of dictionaries
let images = [["title" : "101", "image" : UIImage(named: "1.png")],
["title" : "121", "image" : UIImage(named: "2.png")],
["title" : "122", "image" : UIImage(named: "3.png")],
["title" : "131", "image" : UIImage(named: "4.png")],
["title" : "132", "image" : UIImage(named: "5.png")]]
var filteredImages = [Dictionary]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.resultSearchController.active {
return filteredImages.count
}
else {
return images.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
if self.resultSearchController.active {
let image = filteredImages[indexPath.row]
cell.LabelTitle?.text = image["title"]
cell.CellImage.image = image["image"]
}
else {
let image = images[indexPath.row]
cell.LabelTitle?.text = image["title"]
cell.CellImage.image = image["image"]
}
return cell
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredImages.removeAll(keepCapacity: false)
for imageDictionary in images {
if imageDictionary["title"] == searchController.searchBar.text! {
filteredImages += imageDictionary
}
}
self.tableView.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailedViewController
if let cell = sender as? TableViewCell {
VC.SentData1 = cell.LabelTitle.text
}
}
}
答案 2 :(得分:0)
这将起作用
cell.TitleLabel.text = titleList [indexPath.row] cell.DescriptionLabel.text = DescriptionList [indexPath.row]
let imagename = UIImage(named: ImageList[indexPath.row])
cell.imageView?.image = imagename
return cell