简体   繁体   English

故事板中自定义单元格中的UIActivity指示器

[英]UIActivity indicator in custom cell in storyboard

I have added an IBOutlet of UIActivityIndicator to custom cell but it doesn't show on every cell, just on first 2-3 and then nothing. 我已经在自定义单元格中添加了一个UIActivityIndi​​cator的IBOutlet,但它并没有显示在每个单元格上,只是在前2-3个然后没有。 Another thing, when I scroll the table and get back too first 3 cells, UIActivityIndicator disappears from these cells too... 另一件事,当我滚动表并回到前3个单元格时,UIActivityIndi​​cator也会从这些单元格中消失...

Code: 码:

Cell.h 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 Cell.m

@implementation Cell

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

@end

In storyboard UIactivityIndicator is set to: Behavior: - Animating 在故事板中,UIactivityIndi​​cator设置为:行为: - 动画

for testing purposes... 用于测试目的......

and: 和:

- (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;
}
  1. If you comment out that entire piece of code where you're doing the setImageWithURL , do you get your animated activity indicator successfully on all of the rows of your table? 如果你注释掉你正在执行setImageWithURL整段代码,你是否在表的所有行上成功获得了动画活动指示符? Let's first make sure your UIActivityIndicatorView is working correctly. 让我们首先确保您的UIActivityIndicatorView正常工作。 If it works when you comment out setImageWithURL , then the problem clearly rests there. 如果在注释掉setImageWithURL时它可以正常工作,那么问题显然就在那里了。 If it doesn't work, even when you disable setImageWithURL , then you have a more fundamental issue. 如果它不起作用,即使你禁用setImageWithURL ,那么你有一个更基本的问题。 Let's make sure you are starting the activity indicator successfully. 让我们确保您成功启动活动指示器。 I gather that you're turning it on in IB, but I'd really suggest manually turning it on (when appropriate) in cellForRowAtIndexPath , and not relying upon it being started for you. 我知道你在IB中打开它,但我真的建议在cellForRowAtIndexPath手动打开它(适当时),而不是依赖它为你启动。

  2. In your completed block, you really should be checking to make sure the cell in question is still on screen and hasn't been dequeued and reused (eg invoking cell = [tableView cellForRowAtIndexPath:indexPath] and confirm a non- nil value) because I'm assuming your setImageWithURL is operating asynchronously. 在已完成的块,你真的应该检查,以确保有问题的电池仍然是在屏幕上,并没有被出队并重复使用(如调用cell = [tableView cellForRowAtIndexPath:indexPath]并确认非nil值),因为我假设你的setImageWithURL是异步操作的。 (NB: This UITableView instance method, cellForRowAtIndexPath , should not be confused with the similarly named UITableViewController method.) This sort of logic about assuming the cell is still on screen can be problematic whenever you do async operations on tableview cells. (注意:这个UITableView实例方法, cellForRowAtIndexPath ,不应该与类似命名的UITableViewController方法混淆。)无论何时在tableview单元格上执行异步操作,这种关于假设单元格仍在屏幕上的逻辑都可能会出现问题。 When I hear about the fact that some asynchronous update isn't updating properly, I worry about dequeued cells. 当我听说某些异步更新没有正确更新的事实时,我担心出列的单元格。 (See my point #3 on this Stack Overflow Answer for an example of how to check to see if the cell is still visible; It's a slightly different answer, but gives you an example of the sort of issue you need to be thinking about.) (有关如何检查单元格是否仍然可见的示例,请参阅此Stack Overflow Answer上的第3点;这是一个稍微不同的答案,但为您提供了一个需要考虑的问题类型的示例。 )

  3. Unrelated, but I wouldn't be adding the UITapGestureRecognizer every time. 不相关,但我不会每次都添加UITapGestureRecognizer。 Aren't you risking a cell that's been dequeued and reused a dozen times having a dozen tap gesture recognizers? 难道你不是冒着出了一个已经出列并且重复使用十几次有十几个轻敲手势识别器的细胞吗? Better to do it in your UITableViewCell subclass. 最好在UITableViewCell子类中执行此操作。 I generally do it in layoutSubviews (won't work if you try to do it in your init methods because the imageview won't exist yet). 我通常在layoutSubviews执行它(如果你尝试在init方法中执行它将无法工作,因为imageview还不存在)。

Try... 尝试...

- (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;
}
NINetworkImageView *imageView=[[NINetworkImageView alloc]init];
imageView.delegate=self;
[imageView setPathToNetworkImage:[NSString stringWithStdString:imageUrl]];

Use a third party class to solve this task. 使用第三方类来解决此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM