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
// 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 に答える
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 に答える