首先下载数据然后在TableViewcontroller上显示
我有tableviewcontroller和数据fecthed从服务器。 我用下面的类来下载数据asynchronous。 但我的问题是数据加载时,用户看到tableViewcontroller。 我想在用户看到之前加载数据。
#import <SDWebImage/UIImageView+WebCache.h> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKey:@"name"]; cell.textLabel.font = [UIFont fontWithName:@"BebasNeue" size:24]; cell.textLabel.textColor = [UIColor whiteColor]; NSString *imageURLString=[[tableData objectAtIndex:indexPath.row] valueForKey:@"logo"]; NSString* imageURL = [[tableData objectAtIndex:indexPath.row] valueForKey:@"picture"]; [cell.imageView setImageWithURL:[NSURL URLWithString:imageURL]]; }
解
使用SDWebImage,您可以先下载图像。
哪里?
这取决于你的实现。 也许在以前的控制器,或在你的appDelegate
。 如果tableview
出现在你的初始viewcontroller
,绝对取决于networking的速度。
示例代码
看看这个代码( 从库的自述文件中提取,查看源代码 ):
Manager *manager = [SDWebImageManager sharedManager]; [manager downloadWithURL:imageURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { // YOU DON'T NEED THIS } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (image) { // MAYBE YOU WANT TO DO SOMETHING HERE } }];
然后,在你的tableView:cellForRowAtIndexPath:
方法中,你可以像这样设置图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // ... NSURL* url = GET_IMAGE_URL; [cell.imageView setImageWithURL:url]; // ... }
怎么运行的?
该库负责首先查看其caching,在那里它会find以前下载的图像,因为它使用图像URL作为caching的关键字,以便图像显示在中间。
问题
您正在下载数据并将其加载到tableViewasynchronous,这意味着您的数据进程正在后台执行,但tableView仍处于活动状态,并显示。
我的解决scheme
我以两种方式处理这种情况
-
您首先加载
NSArray
或NSDictionary
中的所有数据(无论您需要什么对象),然后再呈现您的TableViewController
,并将其作为parameter passing给TableViewController
。 换句话说,您需要在呈现tableViewController
之前加载数据。 -
您需要在执行下载数据时创buildanimation视图或加载视图,以告诉用户数据正在加载,因此请等待该过程完成。 当进程完成时,只需重新加载tableView并将数据传递给
TableViewCells
。
例
1)当你发送请求到服务器获取数据(例如来自JSON)时使用此代码。
LoadingView *spinn = [LoadingView loadSpinnerIntoView:self.view]; dispatch_queue_t downloadQueue = dispatch_queue_create("LoadingView", NULL); dispatch_async(downloadQueue, ^{ // do our long running process here [NSThread sleepForTimeInterval:0.1]; // do any UI stuff on the main UI thread dispatch_async(dispatch_get_main_queue(), ^{ //Start downloading your data, and pass data you fetched to objects of NSDictionary //Your methods //After downloading data is done, reload data on tableView [tableView reloadData]; //Remove loading view, and your tableViewController is all set [spinn removeSpinner]; });});
2)从您提出的请求中获取数据NSDictionary
或NSArray
后,您也有链接下载图像。
对于下载图像asynchronous,而不阻止mainThread你可以参考@sonxurxo的问题,或者你可以使用这个也使用SDWebImage
您只需要将#import <SDWebImage/UIImageView+WebCache.h>
到您的项目中,并且只需使用以下代码即可下载图像时的占位符:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; } // Here we use the new provided setImageWithURL: method to load the web image [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; cell.textLabel.text = @"My Text"; return cell; }