Search code examples
iosswiftuitableviewxcode6uisearchdisplaycontroller

Search Display Controller displaying incorrect image


I have the following problem. I have set up a search bar in a tableView written in swift. Each tableView cell has a label and an image. The problem is that when I try to search the searchDisplayController displays the right label but the wrong image (it displays the first image from the array).

when the app loads:

when the app loads

when I try to search:

when I try to search

As you can see the images start over from the beginning and the image view doesn't display the correct image for the iPhone cell.

Thanks in advance for your answers.

EDIT1: cellForRowAtIndexPath code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell

    var friend : FriendItem

    if (tableView == self.searchDisplayController?.searchResultsTableView)
    {
        friend = self.filteredFriends[indexPath.row]

    }
    else
    {
        friend = self.friendsArray[indexPath.row]

    }



    cell.label1.text = friend.name

    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    let item = friendsArray[indexPath.row]
    cell.setCell(item.name, image1: item.image)




    return cell

}

Solution

  • You are taking item from friendsArray, but you should take if from filteredFriends array when you are searching. Try cell.setCell(friend.name, image1: friend.image)