2013-03-19 3 views
0

UILabel 및 UIImageView가 포함 된 사용자 정의 셀이 있습니다. JSON을 사용하여 데이터 검색. 텍스트를 검색 할 때 내 UISearchBar가 완벽하게 작동하지만 UIImageView의 인덱스가 변경되지 않습니다.UIImageView 및 UILabel이 포함 된 사용자 정의 셀에서 UISearchBar 검색

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

} 

제게 조언을주십시오. 이 문서가 문제를 해결하는 것입니다

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

} 

나는 당신의 요구 사항에 따라 코드를 수정했다

+0

아니 좋은 방법 애플을 참조 //developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html –

답변

0

...이 시도 .... HTTPS : jQuery과의 검색과

+0

이것은 훌륭한 솔루션, 그것은 작동합니다 ! @ Bhanu 감사합니다! @Dipen도 솔루션에 감사드립니다! – gokhangultekin

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. 
관련 문제