سؤال

I have a tableview that contains cell images and names as text. At the top i have added a searchbar. And when I search i am able to do the search for names, but the images are still the same. How can change the images also, as per the changes in names when search? this code in the tableview delegate, and i'm using some search delegate methods also. I haven't done anything to arrange the images with respect to the names. I need help to implement that.

if(searching)
        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    else {

        [cell.imageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]  
                   placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
        cell.textLabel.text = [friendsList objectAtIndex:indexPath.row];        
    }

Here is few more code:

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];


    for (NSString *sTemp in friendsList)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

Thanks.

هل كانت مفيدة؟

المحلول

It seems to me that you are not updating your cells correctly.

A simple [myTableView reloadData]; might help, but you might also want to consider looking into the way you are adding the imageViews to you cells. You might not have removed(/released/nilled) the imageViews you place on top of your cells correctly. You have to do this everytime you update your cells as well since the imageViews won't do this themselves unless you specifically told them so.

When you updated your question with some helpful code I can be more specific about this concerning your problem.


Now that you did edit your post with some (although very little amount of) code I can see from this snippet that you are not updating your cell.imageViews to the new list of objects.

You updated your uitableview with the new "copyListOfItems" but you didn't do that same thing for your images resulting in the display of the old content which are the images from "imageArray"

You need to make a similar array for your images as you did with the text.

if(searching)
{
    cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[searchedArrayOfImages objectAtIndex:indexPath.row]];  
}
else {

    [cell.imageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]  
               placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
    cell.textLabel.text = [friendsList objectAtIndex:indexPath.row];        
}

A better way (i.m.o.) to save images to names is making a NSDictionary for them so when you pick a record your name always fits your image.


In regards of the comments below: At the moment you search, mostlikely in the textDidChange delegate method, you mostlikely pick the indexrows of all your search matches. At the moment you pick the index of a hit from "friendlist" and put it into "copyListOfItems" you also need to use the very same index and take the image from "imageArray" and put them into "searchedArrayOfImages". This way you build both your text- and image array the same way. Then when you reload your tableview you can take the same index from "copyListOfItems" and from "searchedArrayOfImages" and populate your cell properly.


It would look something like this:

- (void) searchTableView 
{

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    int indexCount = 0; //keeps track of which index you are at.
    for (NSString *sTemp in friendsList)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
        {
            [copyListOfItems addObject:sTemp]; //this is your name result
            [searchedArrayOfImages addObject:[imageArray objectAtIndex:indexCount]]; //adds image that fits with the nameresult to array of searched images
        }

        indexCount++;
    }

    [searchArray release];
    searchArray = nil;
}

When you are done searching the cells will be updated like this (according to your snippet)

if(searching)
{
    cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[searchedArrayOfImages objectAtIndex:indexPath.row]];  
}
else 
{

    [cell.imageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]  
               placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
    cell.textLabel.text = [friendsList objectAtIndex:indexPath.row];        
}

This should suffice^^ good luck.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top