使用searchbarcontroller进行过滤时重复数据

时间:2014-01-08 05:50:15

标签: ios iphone objective-c uitableview searchbar

我正在使用searchbarcontroller来过滤tableview数据,一切正常,但问题是在使用searchbarcontroller tableview时,在过滤器上重复相同的数据。

喜欢:假设如果在tableview(A,B,C,D)tableViewCells上显示字母表列表,那么当我在此tableview上使用searchbarcontroller时,(tableView == self.searchDisplayController.searchResultsTableView)将以重复方式显示数据

喜欢:如果我正在搜索A,则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;
}

提前致谢。

1 个答案:

答案 0 :(得分: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;