0

I have a custom cell which contains UILabel and UIImageView. Retrieving data with JSON. My UISearchBar working perfectly when searching text but my UIImageView's index is not changing.

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

if (searchText.length == 0) {

    isSearchBarClicked = FALSE;

    [displayItems removeAllObjects];
    [displayItems addObjectsFromArray:userNames];


    [displayPictures removeAllObjects];
    [displayPictures addObjectsFromArray:profilePictures];


} else {

    isSearchBarClicked = true;

    [displayItems removeAllObjects];
    [displayPictures removeAllObjects];

    for (NSString * string in userNames) {

        NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (r.location != NSNotFound) {

            [displayItems addObject:string];

            //I HAVE TO ADD OBJECT TO displayPictures FROM profilePictures IN HERE.

        }

    }

}

[tableView reloadData];

}

Please give me an advice.

Scar
  • 3,460
  • 3
  • 26
  • 51
Gokhan Gultekin
  • 291
  • 5
  • 15
  • Not a good way with search in UITableView,refer Apple:https://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html – LE SANG Mar 19 '13 at 08:17

2 Answers2

0

I had modified your code as per your requirement...Try this...

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    if (searchText.length == 0) {

        isSearchBarClicked = FALSE;

        [displayItems removeAllObjects];
        [displayItems addObjectsFromArray:userNames];


        [displayPictures removeAllObjects];
        [displayPictures addObjectsFromArray:profilePictures];


    } else {

        isSearchBarClicked = true;

        [displayItems removeAllObjects];
        [displayPictures removeAllObjects];
        for(int i = 0; i < [userNames count]; i++) {
            NSString *string = [userNames objectAtIndex:i];
            NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (r.location != NSNotFound) {

                [displayItems addObject:string];

                //I HAVE TO ADD OBJECT TO displayPictures FROM profilePictures IN HERE.
                [displayPictures addObject:[profilePictures objectAtIndex:i]];
            }

        }

    }

    [tableView reloadData];

}

This will solve your problem.

Bhanu Prakash
  • 1,493
  • 8
  • 20
0
// Instead of Storing both the values (username and profilepictures) in two diffrent arrays, Store them into a single Dictionary as follow :

// Step 1:
// Create Mutable Array to store your dictionary object
NSMutableArray *arrUserRecord = [NSMutableArray arrayWithCapacity:0];

// Step 2:
// A Loop through your json Dictionary/Array to Fetch Username and ProfilePicture
for(NSDictionary *record in <YOUR JSON DATA>)
{
    // Store both in single Dictionary
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[record valueForKey:@"username"], @"keyUsername", [record valueForKey:@"picture"], @"keyPicture", nil];

    // Now add this Dictionary to a Mutable Array
    [arrUserRecord addObject:dict];
}

// **** To Display Data ****
// At the End you get an Array of Dictionary which contains username & profilePicture, Use it to display as follow.
NSDictionary *dictDisplay = [arrUserRecord objectAtIndex:indexPath.row];
[cell.textLabel setText:[dictDisplay valueForKey:@"username"]];

// **** To Search Data Use Following ****
if (searchText.length == 0)
{
    // Display all the records
}
else
{
    // Display Searched records
    NSArray *tempArray = arrUserRecord;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username CONTAINS[cd] %@",
                              searchText];

    // Store Search Result
    NSArray *resultArray = [[tempArray filteredArrayUsingPredicate:predicate] mutableCopy];
}

// Update your TableView using following
[tableView beginUpdates];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];


// I hope this will help you. Thank you.
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57