Ios 在执行搜索功能期间,如何实现UIActivityIndicatorView?
我正在构建一个阅读文章的应用程序。我正在以JSON格式从服务器获取数据并加载到UITableViewController。在搜索部分,当用户 为搜索输入内容将数据加载到UITableView需要时间。在加载搜索结果所需的时间之间,我无法实现UIActivityIndicatorView UITableViewController中的数据 这是我的密码:Ios 在执行搜索功能期间,如何实现UIActivityIndicatorView?,ios,json,uitableview,uisearchbar,uiactivityindicatorview,Ios,Json,Uitableview,Uisearchbar,Uiactivityindicatorview,我正在构建一个阅读文章的应用程序。我正在以JSON格式从服务器获取数据并加载到UITableViewController。在搜索部分,当用户 为搜索输入内容将数据加载到UITableView需要时间。在加载搜索结果所需的时间之间,我无法实现UIActivityIndicatorView UITableViewController中的数据 这是我的密码: -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
-(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) {
}
}
}
您可以在单击searchBarSearchButton后立即执行此操作。 然后在cellForRowAtIndexPath中添加此行:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[_activityIndicator startAnimating];
your code
}
下一步:
dataWithContentsOfURL是一个同步API。实现所需功能的一种方法是使用NSURLConnection API从URL异步加载日期。您还应该连接到下面列出的委托方法来启动和停止活动指示器
connection:didReceiveResponse:
connectionDidFinishLoading:
我还想建议您使用像AFNetworking这样的库,它会将前后挂钩作为回调块提供给您,这在编码方面会更干净一些 创建指示器:
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];
请花点时间缩进代码,这会使您的问题变得混乱@Bejiibun,我已经应用了它,但它在表视图中加载数据后工作-voidsearchBarSearchButtonClicked:UISearchBar*搜索栏{[spinner startAnimating];}-UITableViewCell*tableView:UITableView*tableView cellForRowAtIndexPath:NSIndexPath*indexPath{static NSString*Cellidentifier1=@searchCell;YSSearchViewCellTableViewCells*cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier1];长行=[indexPath行];cell.TitleLabel1.text=_Title1[row];cell.AuthorLabel1.text=_Author1[row];if indexPath.row=[u Title1计数]-1{[spinner停止动画制作];}
[activityIndicator stopAnimating];