我是iOS开发的新手。今天我学到了什么是UIActivityIndicatorView。 现在,我正在构建一个项目,我有一个表视图,我已经使用JSON解析填充了表。现在我添加了一个活动指示器,它将旋转到那个时间,直到表格填充为止。 我已经启动了活动指示器,但它没有停止。你能告诉我我哪里错了吗?提前致谢。 这是我的代码。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self fetchData];
self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
_mySpinner.hidden = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) fetchData {
[_mySpinner startAnimating];
NSString *strURL = [NSString stringWithFormat:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
_webData = [NSMutableData data];
}
else{
//error
}
}
#pragma mark Url connection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_webData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
// self.data parse
NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:self.webData options:kNilOptions error:nil];
self.arrDetail = [dict valueForKey:@"loans"];
[self.mySpinner stopAnimating];
self.mySpinner.hidden = YES;
[self.parserTable reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}
#pragma mark Table View Delegates
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
NSDictionary *locationDict = [[self.arrDetail objectAtIndex:indexPath.row]valueForKey:@"location"];
UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = [[self.arrDetail objectAtIndex:indexPath.row]valueForKey:@"name"];
UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2];
lbl2.text = [locationDict valueForKey:@"country"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrDetail count];
}
@end
答案 0 :(得分:2)
解决方案
从代码中删除此行
self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]
问题
您尚未显示mySpinner
的声明声明。但是,从您的代码中我可以看出它是一个IBOutlet,因为您创建了一个UIActivityIndicatorView
的新实例而没有将其添加为子视图,您仍然可以在视图中看到一个活动指示器(因为它出现并且永不停止动画)。
它不会停止动画的原因是您在IBOutlet上调用[_mySpinner startAnimating];
。然后在您说
UIActivityIndicatorView
的新实例
self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]
现在,您在mySpinner
上调用的任何方法都会在一个活动指示符上调用,该指标不在您的视图中,而是您创建的那个,因为您在创建新{{1}时失去了对它的引用}。
另外,既然你是新人。我建议您尽可能使用UIActivityIndicatorView
,不要互换使用self.mySpinner
和self
,因为两者都可以根据需要使用。原因超出了你的问题的范围。
答案 1 :(得分:1)
启动活动指示器:
[cell.indicater startAnimating];
启动活动指示器:
[cell.indicater stopAnimating];
还设置截图中显示的属性。
答案 2 :(得分:1)
由于您要为您的outlet属性分配UIActivityIndicatorView
的新实例,因此对InterfaceBuilder中的一个实例的引用将丢失。
只需删除:self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
答案 3 :(得分:0)
首先初始化您的微调器,然后调用您的连接方法,如下所示。
...
- (void)viewDidLoad {
[super viewDidLoad];
self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
_mySpinner.hidden = NO;
_mySpinner.center = self.view.center;
[self fetchData];
}
...