Ios 在uisearchbar中键入文本时,uitableview不会重新加载到最新数组
我正在使用这种方法从foursquare获得场地。当我点击搜索栏加载场地时,它会显示表格减去一次迭代(例如,如果我搜索“SAMSUNG”,它会重新加载“SAMSUN”的tableview),但我可以从nslog中看到,数据是从four square检索的,并存储在nsarray中,但它不会显示在表格中Ios 在uisearchbar中键入文本时,uitableview不会重新加载到最新数组,ios,uisearchbar,Ios,Uisearchbar,我正在使用这种方法从foursquare获得场地。当我点击搜索栏加载场地时,它会显示表格减去一次迭代(例如,如果我搜索“SAMSUNG”,它会重新加载“SAMSUN”的tableview),但我可以从nslog中看到,数据是从four square检索的,并存储在nsarray中,但它不会显示在表格中 - (void)getFourSquare:(NSString *)SearchString { NSString *CLIENT_ID = @"CLIENT_ID"; NSString *CLI
- (void)getFourSquare:(NSString *)SearchString
{
NSString *CLIENT_ID = @"CLIENT_ID";
NSString *CLIENT_SECRET = @"CLIENT_SECRET";
NSString *searchString = SearchString;
NSString *requestString =[NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?near=perth&intent=browse&radius=20000&limit=25&query=%@&client_id=%@&client_secret=%@&v=20140710",searchString,CLIENT_ID, CLIENT_SECRET];
NSURL *urlString = [NSURL URLWithString:requestString];
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:urlString] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error) {
NSArray *latestFSquares = [self fetchData:data];
fsquares = [NSMutableArray arrayWithCapacity:10];
if (latestFSquares) {
for (NSDictionary *fSquareDic in latestFSquares) {
foursquare *fsquare = [[foursquare alloc] init];
fsquare.name = [fSquareDic objectForKey:@"name"];
NSLog(@"name : %@ ",[fSquareDic objectForKey:@"name"]);
[fsquares addObject:fsquare];
};
}
}
[self.tableView reloadData];
}];
}
下面是我用来更新搜索文本的方法,我尝试在下面的函数中使用tableView重新加载,但仍然显示了减去一次迭代
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self getFourSquare:searchText];
}
问题就在下面这行
fsquares = [NSMutableArray arrayWithCapacity:10];
您每次都在创建NSMutableArray
的新引用,这是错误的。要修复此问题,请初始化fsquares
一次并更改如下值
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(!fsquares)
fsquares = [NSMutableArray arrayWithCapacity:10];
[self getFourSquare:searchText];
}
并删除函数中的初始化getFourSquare
更新
将[fsquares removeAllObjects]
移动到下面块中的下一行
if (latestFSquares) {
[fsquares removeAllObjects];
}
它应该会修复
干杯。@IOSFeng那么很可能您没有获得数据,我们正在删除它,在它获得搜索结果之前,请参阅我的更新我已将removeobjects函数移动到它获取要加载的数据的时间。。它现在应该可以工作了。它仍然是完全空白的,上面的问题加载了uitableview,问题是它没有将其更新为完整的搜索查询。如果我搜索“SAMSUNG”,它会重新加载“SAMSUN”的tableview