【发布时间】:2015-02-12 11:05:44
【问题描述】:
情况:
对于 UITableView,我使用的是过滤器文本视图。 此表格视图包含世界各国。当搜索值长度 >= 4 时,应该开始智能搜索。此智能搜索使用 iOS CLGeocoder,因此您还可以使用同义词或首都来查找国家/地区。
我的问题:
每次文本更改时,都会调用 uitableview 的方法“reloadData”。 当我在 uitableview 的 textChange 方法和 numberOfRowsInSection 方法上设置断点时,它们都被命中。 但是当我想输入 London 时,正常(不智能)搜索在“Lon”处没有任何结果。当我输入第四个字母时(导致'Lond'),textChange 断点被命中,但 numberOfRowsInSection 断点不是。
当先前的 reloadData 导致没有行或其他什么时,reloadData 是否可能不会更新 tableview?我不知道确切的问题是什么,有认识这种情况的人吗?
代码:
-(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 方法创建一个谓词,但该方法有效,所以我没有在此处包含它。
【问题讨论】:
-
你能添加一些你正在使用的代码吗???
-
添加你的代码是有意义的。
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。跨度>
-
对不起,我以为这只是基本的 UITableView 代码,所以我没有添加它。现在我做到了!
-
@harmjanr 你知道
geocodeAddressString:completionHandler:执行网络调用吗?你确定你在等待那个异步块返回吗?
标签: ios objective-c uitableview