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;
}
在此先感谢。
你检查你滤波阵列欲望秩序? – Retro