在autolayout下,如何在UIImageView有可用图像之前显示加载轮?

时间:2014-10-04 01:37:06

标签: ios storyboard autolayout

我有很多使用硬编码帧的老式代码,我正在用故事板和自动布局替换它。

此旧代码的一部分会在从Internet检索图像时在表格单元格中显示UIActivityIndicatorView。图像准备好后,加载轮从其超视图中移除,并替换为保留新图像的UIImageView

如何使用故事板和自动布局实现相同的目标?

3 个答案:

答案 0 :(得分:0)

  1. 创建活动指示器插座并相应地分配约束。
  2. 然后,当您开始加载图片时,请在完成后运行[[self activityIndicator] startAnimating][[self activityIndicator] stopAnimating]

答案 1 :(得分:0)

正在删除并添加要求吗?看起来隐藏会更容易。

将两个视图放在Interface Builder的原型单元格中并设置其约束。使用自定义单元格类来保存图像和活动视图(我将其称为我的JLTTableViewCell)。作为此设置的一部分,我想象一个JLTDataObject有一个图像和一个加载使用完成块的图像的方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JLTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    JLTDataObject *object = self.data[indexPath.row];

    cell.activityIndicatorView.hidden = object.image != nil;
    cell.loadedImageView.hidden = object.image == nil;
    cell.loadedImageView.image = object.image;

    if (object.image == nil) {
        [cell.activityIndicatorView startAnimating];
        [object loadImageWithCompletion:^{
            [cell.activityIndicatorView stopAnimating];
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    }

    return cell;
}

这将首先加载隐藏图像视图并显示活动指示符的单元格。然后它加载图像。完成后,它会重新加载该单元格。这会导致再次调用-tableView:cellForRowAtIndexPath:,但这次加载了图像。

在第二次通过时,图像存在,因此显示图像视图并隐藏活动指示器。

答案 2 :(得分:-1)

您需要在后台队列中编写代码,因为数据将快速加载,但加载图像可能需要一些时间 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arrImages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
NSString * cellIdentifire = @“tblCell”;    
CustomCell * Cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifire];     
if(Cell == nil){       
Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
    }
    [Cell.contentLabel setText:[NSString stringWithFormat:@“Index Path:%d”,(int)indexPath.row]];
    [Cell.viewImage setBackgroundColor:[UIColor blackColor]];
    if(Cell.viewImage.image == nil){
        [Cell.indicateActivate startAnimating];
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),^ {
        NSURL * url = [NSURL URLWithString:[arrImages objectAtIndex:indexPath.row]];
        NSData * imgData = [NSData dataWithContentsOfURL:url];
        [Cell.viewImage setImage:[UIImage imageWithData:imgData]];
        if(Cell.viewImage.image!= nil){
            [Cell.indicateActivate stopAnimating];
        }
    });

return Cell;<br>

}

- (void)viewDidLoad {    
[super viewDidLoad];     //在加载视图后进行任何其他设置,通常是从笔尖。     
arrImages = @ [@“http://img4.wikia.nocookie.net/__cb20110302225528/rift/images/7/7f/AWESOME_FACE!!!.png”,                   @ “http://t3.gstatic.com/images?q=tbn:ANd9GcTjBSUc3q9eHq5b7uby1wPEowXQW_gAUWi3uKmMlgv8Vz5diwAKDA”,                   @ “http://t1.gstatic.com/images?q=tbn:ANd9GcTwMjSiu59v8eqNC8W19lGeqynFVXeZEnLuV0qhJNdk3La_M-sp”,                   @ “http://upload.wikimedia.org/wikipedia/commons/6/6a/Tricoloring.png”,                   @ “http://t1.gstatic.com/images?q=tbn:ANd9GcQSwRRSVAmwLBsO-Qdp4r0uPbppPviYFDCiw9DP8v8_bL4uWUF_”,                   @ “http://img1.wikia.nocookie.net/__cb20131105231053/avp/images/thumb/4/4a/Photographer_Barnstar.png/505px-Photographer_Barnstar.png”,                   @ “http://t2.gstatic.com/images?q=tbn:ANd9GcQmWPQ32AfUo6PnGujNX3rR6FTPiICnNqQP7r35XjmzEjWiRJ4y”];
}

在属性检查器中为活动指示器设置“已停止时隐藏”。 enter image description here