0

我有一个包含 UILabel 和 UIImageView 的自定义单元格。使用 JSON 检索数据。我的 UISearchBar 在搜索文本时工作正常,但我的 UIImageView 的索引没有改变。

-(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];

}

请给我一个建议。

4

2 回答 2

0

我已根据您的要求修改了您的代码...试试这个...

-(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];

}

这将解决您的问题。

于 2013-03-19T09:05:58.030 回答
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.
于 2013-03-19T09:48:22.967 回答