2014-01-08 44 views
0

我使用searchbarcontroller篩選tableview數據,一切工作正常,但問題是相同的數據在篩選器上重複使用searchbarcontroller tableview。數據重複使用searchbarcontroller篩選

喜歡:假設如果字母的列表被顯示在所述的tableview(A,B,C,d)tableViewCells然後當我在此的tableview然後(tableView == self.searchDisplayController.searchResultsTableView)是用於searchbarcontroller示出了數據以重複方式

喜歡:如果我正在搜索searchResultTableView在tableViewCells上顯示(A,A,A,A,A,A)。

但是在正常的tableview數據顯示正常沒有重複的數據。

任何人都請告訴我我的代碼中的實際問題在哪裏。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.xyz.com/work.php]]; 
    responseDataSP=[[NSMutableData alloc]init]; 
    urlConnectionSP=[[NSURLConnection alloc]initWithRequest:request delegate:self]; 
    NSLog(@"nsurl my %@",urlConnectionSP); 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 

    NSLog(@"Error"); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 

    [responseDataSP setLength:0]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

    [responseDataSP appendData:data]; 
    NSLog(@"%@",responseDataSP); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    NSError *error=nil; 
    NSDictionary *dicSP=[NSJSONSerialization JSONObjectWithData:responseDataSP options:NSJSONReadingMutableContainers error:&error]; 
    self.totalDataSP=[dicSP objectForKey:@"data"]; 
    NSLog(@"%@",_totalDataSP); 



    totalTitleSP=[[NSMutableArray alloc]init]; 
    totalImageSP=[[NSMutableArray alloc]init]; 
    totalIdWalaSP=[[NSMutableArray alloc]init]; 


    [_tableView reloadData]; 

} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [searchResults count]; 

    } else { 
     return [_totalDataSP count]; 

    } 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 80; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SkillsTableCell"; 

    SkillsTableCell *cell = (SkillsTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SkillsTableCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 



    } 



    NSDictionary *items=nil; 


    NSArray *titleSP; 
    NSArray *imageSP; 
    NSArray *idwalaSP; 

    for(int i=0;i<[_totalDataSP count];i++){ 

    // NSLog(@"the _totalDataSP is %@",[_totalDataSP count]); 

     items=[_totalDataSP objectAtIndex:i]; 

     titleSP=[items objectForKey:@"first_name"]; 
     [totalTitleSP addObject:titleSP]; 


     imageSP=[items objectForKey:@"profile_pic"]; 
     [totalImageSP addObject:imageSP]; 

     idwalaSP=[items objectForKey:@"id"]; 
     [totalIdWalaSP addObject:idwalaSP]; 


     category_id = [items objectForKey:@"id"]; 
     NSLog(@"id value is %@",category_id); 

    } 



    dispatch_async(dispatch_get_main_queue(), ^{ 

     if (tableView == self.searchDisplayController.searchResultsTableView) { 
      cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; 
     } else { 
      cell.nameLabel.text=[totalTitleSP objectAtIndex:indexPath.row]; 
     } 



     //For Image 
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[totalImageSP objectAtIndex:indexPath.row]]]; 
     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
      if (data) { 

       cell.thumbnailImageView.image = [UIImage imageWithData:data]; 
      } 
     }]; 

     [cell setNeedsDisplay]; 
    }); 

    return cell; 
} 


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate 
            predicateWithFormat:@"SELF contains[cd] %@", 
            searchText]; 

    searchResults = [totalTitleSP filteredArrayUsingPredicate:resultPredicate]; 
} 

#pragma mark - UISearchDisplayController delegate methods 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 

在此先感謝。

+0

你檢查你濾波陣列慾望秩序? – Retro

回答

0

使用謂詞使用數組

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'e'"]; 
    [array filterUsingPredicate:sPredicate]; 

它顯示沒有重複

NSArray *beginArray = [filteredArray filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat: 
    @"self BEGINSWITH[cd] %@", searchText]]; 
NSArray *anyArray = [filteredArray filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat: 
    @"self CONTAINS[cd] %@", searchText]]; 

NSMutableArray *resultArray = [NSMutableArray arrayWithArray:beginArray]; 
for (id obj in anyArray) { 
    if (![resultArray containsObject:id]) { 
     [resultArray addObject:id]; 
    } 
} 
filteredArray = resultArray;