Ios 在“自动布局”下,在UIImageView具有可用图像之前,如何显示加载控制盘?

Ios 在“自动布局”下,在UIImageView具有可用图像之前,如何显示加载控制盘?,ios,storyboard,autolayout,Ios,Storyboard,Autolayout,我有很多使用硬编码框架的老式代码,我正在用故事板和自动布局来取代它 这段旧代码的一部分将在从Internet检索图像时在表单元格中显示UIActivityIndicatorView。图像准备好后,加载轮将从superview中移除,并替换为保存新图像的UIImageView 如何使用故事板和自动布局实现同样的效果 创建活动指示器出口并相应地分配约束 然后,当您开始加载图像时,在图像完成后运行[[self-activityIndicator]startAnimating]和[[self-activ

我有很多使用硬编码框架的老式代码,我正在用故事板和自动布局来取代它

这段旧代码的一部分将在从Internet检索图像时在表单元格中显示
UIActivityIndicatorView
。图像准备好后,加载轮将从superview中移除,并替换为保存新图像的
UIImageView

如何使用故事板和自动布局实现同样的效果

  • 创建活动指示器出口并相应地分配约束
  • 然后,当您开始加载图像时,在图像完成后运行
    [[self-activityIndicator]startAnimating]
    [[self-activityIndicator]stop animating]

  • 删除和添加是一项要求吗?看来躲藏起来会容易些

    将这两个视图放在Interface Builder的prototype单元中,并设置它们的约束。使用自定义单元格类保存图像和活动视图(我将调用我的
    jltableviewcell
    )。作为这个设置的一部分,我将想象一个
    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:
    ,但这次将加载图像


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

    您需要在后台队列中编写代码,因为数据加载速度很快,但加载图像可能需要时间。
    -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
    返回图像。计数;
    }
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    NSString*cellIdentifire=@“tblCell”;
    CustomCell*Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    如果(单元格==nil){
    Cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
    }
    [Cell.contentLabel setText:[NSString stringWithFormat:@“索引路径:%d”,(int)indexath.row];
    [Cell.viewImage setBackgroundColor:[UIColor blackColor]];
    如果(Cell.viewImage.image==nil){
    [Cell.indicatateActivate startAnimating];
    }
    调度异步(调度获取全局队列(调度队列优先级后台,0),^{
    NSURL*url=[NSURL URLWithString:[arrmages objectAtIndex:indexath.row]];
    NSData*imgData=[NSData datawithcontentsofull:url];
    [Cell.viewImage setImage:[UIImage imageWithData:imgData]];
    如果(Cell.viewImage.image!=nil){
    [Cell.indicatateActivate停止动画制作];
    }
    });

    返回单元格;
    }

    -(无效)viewDidLoad{
    [超级视图下载]; //加载视图后,通常从nib执行任何其他设置。
    arrImages=@[@”“, @"", @"", @"", @"", @"", @“”];
    }

    设置在活动指示器的属性检查器中停止时隐藏。

    谢谢。出于某种原因,我认为自动布局不允许我在图像视图上叠加活动指示器。但确实如此。
    return Cell;<br>