B.S*_*mar 6 objective-c uitableview uisearchbar ios
我会在我的应用程序中实现SearchBar概念,但它有一个错误,当我输入搜索栏中的第一个字符时它不工作而tableview没有显示相关字段,它显示空tableview但我输入第二个字符显示相关字段在tableview中,我不知道是什么错误让我请帮帮我.
我在这里给出了我的代码.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"searching::");
if(searchBar.text.length == 0)
{
[searchDict removeAllObjects];
[arrayDict removeAllObjects];
searchDict=[[NSMutableArray alloc]init];
arrayDict=[[NSMutableArray alloc]init];
[self getJsonResponse];
[self.tableView reloadData];
} else {
[arrayDict removeAllObjects];
arrayDict=[[NSMutableArray alloc]init];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self getSearchJsonResponse:searchBar.text];
NSLog(@"searchDict::%@",searchDict);
[self.tableView reloadData];
});
}
Run Code Online (Sandbox Code Playgroud)
根据聊天中的代码,您需要彻底检查代码是否存在内存泄漏和性能问题。它到处都是。
抛开这些,进行以下更改以使您的代码有机会工作:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(searchBar.text.length == 0) {
[searchDict removeAllObjects];
[arrayDict removeAllObjects];
[self getJsonResponse];
}
else {
[arrayDict removeAllObjects];
[self getSearchJsonResponse:searchBar.text];
}
}
Run Code Online (Sandbox Code Playgroud)
另外,将[self.tableView reloadData]调度包装到 Web 服务的完成块中的主队列中,如下所示:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
Run Code Online (Sandbox Code Playgroud)
另外,记录什么NSLog(@"SEARCHDICT::%@",searchDict);?您缺乏错误处理是否有可能会吞掉失败的 Web 服务调用响应(例如,空搜索栏或“我不是在搜索长度 1”)?
| 归档时间: |
|
| 查看次数: |
661 次 |
| 最近记录: |