1

我正在尝试在 [self.tableview reloaddata] 之前的 viewDidLoad() 函数中添加加载数据选项。我不确定如何添加它,有没有办法让用户知道有数据正在加载。我正在解析 JSON 文件,并且数据在 3G 上加载速度很慢,因此它是让用户知道正在使用加载选项加载数据的更好方法。这是我的代码:

- (void)viewDidLoad {
  [super viewDidLoad];


// Add the view controller's view to the window and display.
responseData = [[NSMutableData data] retain];
self.twitterArray = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp=5"]];


[[NSURLConnection alloc] initWithRequest:request delegate:self];   

[super viewWillAppear:animated];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  [responseData setLength:0];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   [responseData appendData:data];
 }


 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

   NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  
   [responseData release];  

   NSDictionary *results = [responseString JSONValue];  

   self.twitterArray = [results objectForKey:@"results"]; 

   [self.tableView reloadData]; // How to add loading view before this statement

}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   // Return the number of rows in the section.

   return [self.twitterArray count];
 }
4

1 回答 1

1

我不太确定您所说的“加载视图”是什么意思,但我想您的意思是活动指示器或其他在加载数据时应该显示的内容。

  1. 做一个伊瓦尔UIActivityIndicatorView *myLoadingView;
  2. 在 viewDidLoad 中初始化 myLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; myLoadingView.hidesWhenStopped = YES; [myLoadingView stopAnimating]; [self.view addSubView:myLoadingView];
  3. 在开始连接之前显示视图 [myLoadingView startAnimating];
  4. 下载完成后通过在委托方法 connectionDidFinishLoading: after [self.tableView reloadData] 中停止它再次隐藏它;[myLoadingView stopAnimating];
  5. 在 viewDidUnload 中释放它[myLoadingView release];

如果您有任何问题或我是否误解了您,请随时询问。

于 2011-07-30T22:31:12.300 回答