我的应用程序使用 SearchGraphical 在应用程序中搜索注册用户......我的问题是,当我去做研究时,我会看到重复的结果:
名称搜索: Francesca 结果: Francesca (1 cell) Francesca (2 Cell)
**09/14/2013 22:18:17.817 Unipot v.02 [63396: a0b] Successfully retrieved 1 scores.
09/14/2013 22:18:17.818 Unipot v.02 [63396: a0b] Content: Antonella Folino
09/14/2013 22:18:17.818 Unipot v.02 [63396: a0b] Content: francesca Folino
09/14/2013 22:18:17.818 Unipot v.02 [63396: a0b] Content: francesca Folino
09/14/2013 22:18:17.819 Unipot v.02 [63396: a0b] Content: francesca Folino**
我不明白为什么。当您在搜索栏中快速完成键入时会出现此问题。
此外,应用程序会在短时间内返回此通知而崩溃:
**Terminating apt two to uncaught exception 'NSRangeException', reason: '*** - [__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9] '**
这是我的文件。M 你能帮帮我吗?
#import "Ricerca.h"
#import "Custom.h"
@interface Ricerca () <UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;
@end
@implementation Ricerca
- (void)viewDidLoad {
[super viewDidLoad];
[self loadObjects];
self.searchResults = [NSMutableArray array];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.parseClassName = @"_User";
self.paginationEnabled = YES;
self.loadingViewEnabled = NO;
self.objectsPerPage = 10;
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
// Configure the cell
UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
cell.detailTextLabel.backgroundColor = color;
cell.textLabel.backgroundColor = color;
if (tableView == self.tableView) {
cell.textLabel.text = [object objectForKey:@"username"];
}
else {
PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
NSString *content = [searchedUser objectForKey:@"username"];
cell.textLabel.text = content;
NSLog(@"Content: %@", content);
}
return cell;
}
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"username" containsString:searchTerm];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d scores.", objects.count);
[self.searchResults addObjectsFromArray:objects];
[self.searchDisplayController.searchResultsTableView reloadData];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.tableView) {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
} else {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}