在Storyboard中自定义单元格中的UIActivity指示器

3

我已经在自定义单元格中添加了一个UIActivityIndicator的IBOutlet,但它并没有显示在每个单元格上,只有前面的2-3个单元格上才有。另外,当我滚动表格并回到前3个单元格时,UIActivityIndicator也会从这些单元格中消失...

代码:

Cell.h

@interface Cell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@end

Cell.m

@implementation Cell

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        // change to our custom selected background view
    }
    return self;
}

@end

在Storyboard中,UIActivityIndicatorView被设置为: 行为: - 动画
用于测试目的...
并且:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // load the image for this cell
    Wallpaper *wallpaper = [xmlParser.wallpapers objectAtIndex:indexPath.row];

    cell.label.text = wallpaper.title;
    NSString *imageToLoad = wallpaper.thumbnail;

    [cell.image setImageWithURL:[NSURL URLWithString:imageToLoad] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize)
     {

     }
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {
         //[cell.activityIndicator stopAnimating];
     }];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self action:@selector(tapped:)];
    [cell.image addGestureRecognizer:tap];
    [cell.image setTag:indexPath.row];

    CALayer * l = [cell.image layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:5.0];
    cell.layer.cornerRadius = 7.0;


    return cell;
}

您可能还想从代码中创建UIIndicatorView,而不仅仅是从IB创建。听起来重用不正常或未在IB中分配给单元格。这里可能会发生几件事情。 - Mark McCorkle
是的,我知道UITapGestureRecognizer,我已经尝试将属性(nonatomic, strong)UITapGestureRecognizer *tap;设置到头文件中,并在viewDidLoad中加载它,但它并不起作用,不能在每个单元格上工作。 Rob,你有Skype吗?我真的需要帮助解决这个问题,我已经卡了两天了。 - 1337code
@MarkM,它被分配给IBOutlet,但仅适用于前3个单元格,我将尝试在代码中创建它。 - 1337code
3个回答

1
如果您将执行setImageWithURL的整个代码段注释掉,您是否能够在表格的所有行上成功显示动画活动指示器?让我们首先确保您的UIActivityIndicatorView正常工作。如果您注释掉setImageWithURL后它可以正常工作,则问题明显存在于那里。如果即使在禁用setImageWithURL时仍无法正常工作,则您可能有更根本的问题。让我们确保您已成功启动了活动指示器。我了解您正在IB中打开它,但我真的建议您在cellForRowAtIndexPath中手动打开它(当适当时),而不是依赖于自动启动。
在您的完成块中,您应该确保所讨论的单元格仍然在屏幕上,并且还没有被取消队列和重用(例如,调用cell = [tableView cellForRowAtIndexPath:indexPath]并确认非nil值),因为我假设您的setImageWithURL是异步操作。(注:此UITableView实例方法cellForRowAtIndexPath不应与同名的UITableViewController方法混淆)。这种关于假定单元格仍然在屏幕上的逻辑可能在您对表格视图单元格执行异步操作时会出现问题。当我听说某些异步更新无法正确更新时,我担心已取消队列的单元格。(请参阅我在Stack Overflow Answer上的第3点,以获取有关如何检查单元格是否仍可见的示例;这是一个稍微不同的答案,但它为您提供了需要考虑的问题类型的示例。)
与此无关,但我不会每次都添加UITapGestureRecognizer。您不是冒着被重用数十次的单元格具有十几个轻击手势识别器的风险吗?最好在UITableViewCell子类中完成此操作。通常我在layoutSubviews中完成它(如果您尝试在init方法中执行此操作,将不起作用,因为图像视图还不存在)。

0

尝试...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     static NSString *CellIdentifier = @"CustomCell";
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.activityIndicator.hidden = NO;
    [cell.activityIndicator startAnimating];
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.1]];

    // load the image for this cell
    Wallpaper *wallpaper = [xmlParser.wallpapers objectAtIndex:indexPath.row];

    cell.label.text = wallpaper.title;
    NSString *imageToLoad = wallpaper.thumbnail;

    [cell.image setImageWithURL:[NSURL URLWithString:imageToLoad] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize)
     {

     }
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {
         //[cell.activityIndicator stopAnimating];
     }];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                 initWithTarget:self action:@selector(tapped:)];
    [cell.image addGestureRecognizer:tap];
    [cell.image setTag:indexPath.row];

    CALayer * l = [cell.image layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:5.0];
    cell.layer.cornerRadius = 7.0;


    return cell;
}

0
NINetworkImageView *imageView=[[NINetworkImageView alloc]init];
imageView.delegate=self;
[imageView setPathToNetworkImage:[NSString stringWithStdString:imageUrl]];

使用第三方类来解决这个任务。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接