我正在构建一篇阅读app的文章。我以JSON格式从服务器获取数据并加载到UITableViewController。
在用户搜索部分
为搜索输入内容需要时间将数据加载到UITableView中。
我无法在搜索结果加载之间实现UIActivityIndicatorView
UITableViewController中的数据。
这是我的代码:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *searchText=searchBar.text;
[self.mySearchBar resignFirstResponder];
NSString *trimmedString = [searchText stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
if (trimmedString.length==0) {
isFilter=NO;
UIAlertView *noConn = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Please enter your something in search bar" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
[noConn show];
}
else
NSString *searchNew = [trimmedString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
isFilter=YES;
@try {
[label removeFromSuperview];
_Title1 = [[NSMutableArray alloc] init];
_Author1 = [[NSMutableArray alloc] init];
_Images1 = [[NSMutableArray alloc] init];
_Details1 = [[NSMutableArray alloc] init];
_link1 = [[NSMutableArray alloc] init];
_Date1 = [[NSMutableArray alloc] init];
NSString* myURLString = [NSString stringWithFormat:@“www.example.com&search=%@", searchNew];
NSURL *url = [NSURL URLWithString:myURLString];
NSData* data = [NSData dataWithContentsOfURL:url];
if ((unsigned long)data.length > 3)
{
NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(ys_avatars){
for (int j=0;j<ys_avatars.count;j++)
{
[_Title1 addObject:ys_avatars[j][@"title"]];
[_Author1 addObject: ys_avatars[j][@"author"]];
[_Images1 addObject: ys_avatars[j][@"featured_img"]];
[_Details1 addObject:ys_avatars[j][@"content"]];
[_link1 addObject:ys_avatars[j][@"permalink"]];
NSString *newStr=[ys_avatars[j][@"date"] substringToIndex:[ys_avatars[j] [@"date"] length]-3];
[_Date1 addObject:newStr];
}
}
else
{
NSLog(@"error");
}
[self.myTableView reloadData];
}
}
else{
[self.myTableView reloadData];
self.tableView.separatorColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
label = [[UILabel alloc] initWithFrame:CGRectMake(90, 100, 200, 100)];
label.text=@"No Article Found";
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
}
}
@catch (NSException *exception) {
}
}
}
答案 0 :(得分:0)
您可以在单击searchBarSearchButton后立即执行此操作。 然后在cellForRowAtIndexPath中添加这一行:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[_activityIndicator startAnimating];
your code
}
下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexpath.row == [your_array_of_content count]-1)
{
[_activityIndicator stopAnimating];
}
}
答案 1 :(得分:0)
dataWithContentsOfURL是一个同步API。实现所需目标的一种方法是使用NSURLConnection API以异步方式从URL加载日期。您还应该挂钩下面列出的委托方法来启动和停止活动指示器。
connection:didReceiveResponse:
connectionDidFinishLoading:
我还建议你使用像AFNetworking这样的库,它会给你前后钩子作为回调块,这在编码方面会更加清晰。
答案 2 :(得分:0)
创建指标:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setCenter:CGPointMake(view.frame.size.width/2.0, view.frame.size.height/2.0)];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.color = [UIColor blackColor];
//add it to the view
[self.view addSubview:activityIndicator];
//start the animation
[activityIndicator startAnimating];
当你想要解雇它时:
[activityIndicator stopAnimating];