Simen
Simen

Reputation: 85

Pictures in tableview change when use search bar

I have created a TableView with a cell containing a label and a image. When i don't use the Search Bar everything works fine, But when I use the Search Bar picture 1.png is always showed first. Is it a way to connect the pictures to the labels when using search bar? Thanks for your help

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
        }
    }
}

Upvotes: 0

Views: 615

Answers (3)

CodeOne
CodeOne

Reputation: 1

this will work

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

Upvotes: 0

Russell
Russell

Reputation: 3089

Here's the example of your code using a dictionary. I haven't checked or compiled this using Xcode but the general ideas are there for you to see.

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
        }
    }
}

Upvotes: 0

Russell
Russell

Reputation: 3089

When you search, the filtered array will only have the matched titles. There is most likely only one match, so filteredfarger.count is 1.

When cellForRowAtIndexPath is called while searching, you generate the image according to UIImage(named: ImageList[indexPath.row]). Since filteredfarger.count is 1, only 1 row will be created and similarly indexPath.row will be 0. This will result in image 1.png always being referenced from the ImageList array.

Upvotes: 1

Related Questions