【问题标题】:Is there a way to add Loading View before reload tableview data有没有办法在重新加载表格视图数据之前添加加载视图
【发布时间】:2011-07-30 21:08:41
【问题描述】:

我正在尝试在 [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];
 }

【问题讨论】:

    标签: iphone objective-c json uitableview uiprogressview


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2019-06-10
      • 1970-01-01
      相关资源
      最近更新 更多