情况:
对于UITableView,我使用的是过滤器文本视图。这张表包含了全世界的国家。当搜索值长度为>= 4时,应该启动智能搜索。此智能搜索使用iOS CLGeocoder,因此您还可以使用同义词或首都来查找国家。
我的问题是:
每次文本更改时,都会调用uitableview的方法'reloadData‘。当我在uitableview的textChange方法和numberOfRowsInSection方法上设置一个断点时,它们都会被击中。但是当我想输入伦敦时,普通的(非聪明的)搜索在'Lon‘上没有任何结果。当我输入第四个字母(导致'Lond')时,textChange断点被击中,但numberOfRowsInSection断点没有。
当上一个reloadData没有生成任何行或其他东西时,是否可能reloadData不更新表视图?我不知道到底是什么问题,有谁认识到这种情况吗?
守则:
-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
filterText = searchText;
//smart search
if([searchText length] >= 4) {
[self smartSearch:searchText];
}
[_contentTableView reloadData];
}
-(void) smartSearch:(NSString*) text {
CLGeocoder* gc = [[CLGeocoder alloc] init];
[gc geocodeAddressString:text completionHandler:^(NSArray *placemarks, NSError *error)
{
smartSearchResult = [[NSMutableArray alloc] init];
for(CLPlacemark *mark in placemarks) {
[smartSearchResult addObject:mark.country];
NSLog(@"Smart search result: %@", mark.country);
}
[_contentTableView reloadData];
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSPredicate *predicate = [self createFilterPredicate];
NSArray *filtered = [countries filteredArrayUsingPredicate:predicate];
return filtered.count;
}方法createFilterPredicate创建了一个谓词,但是该方法可以工作,所以这里没有包含它。
发布于 2015-02-13 13:00:52
您正在创建一个从未使用过的数组(smartSearchResult)。您应该有一个属性声明来存储这些项,以便您可以在UITableViewDataSource方法中访问它。否则,您在geocodeAddressString:completionHandler:中收集的数据将丢失,您仍将从numberOfSectionsInTableView:返回0。
像这样的东西应该能起作用(我不得不对您的其他类结构做一些猜测):
@interface TableViewController : UITableViewController {
}
@property (strong, nonatomic) NSArray *dataSource;
@property (copy , nonatomic) NSString *filterText;
@end
@implementation TableViewController
#pragma mark -
#pragma mark - UITableViewDataSource Protocol Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (self.dataSource == nil) {
return 0;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSPredicate *predicate = [self createFilterPredicate];
NSArray *filtered = [countries filteredArrayUsingPredicate:predicate];
return filtered.count;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
self.filterText = searchText;
//smart search
if (searchText.length > 3) {
[self smartSearch:searchText];
}
[self.tableView reloadData];
}
- (void)smartSearch:(NSString *) text {
CLGeocoder* gc = [[CLGeocoder alloc] init];
[gc geocodeAddressString:text completionHandler:^(NSArray *placemarks, NSError *error) {
NSMutableArray *smartSearchResult = [[NSMutableArray alloc] init];
for (CLPlacemark *mark in placemarks) {
[smartSearchResult addObject:mark.country];
NSLog(@"Smart search result: %@", mark.country);
}
[self.tableView reloadData];
}
];
}
@endhttps://stackoverflow.com/questions/28475844
复制相似问题