3

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. Another thing, when I scroll the table and get back too first 3 cells, UIActivityIndicator disappears from these cells too...

Code:

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

In storyboard UIactivityIndicator is set to: Behavior: - Animating

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;
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1337code
  • 385
  • 1
  • 6
  • 19
  • You may also want to create the UIIndicatorView from code rather than from IB as well. Sounds like the reuse isn't working properly or is not assigned in IB to the cell. There are a couple things that could be happening here. – Mark McCorkle Dec 10 '12 at 20:20
  • Yes, I am aware of the UITapGestureRecognizer, I have tried setting a property (nonatomic, strong) UITapGestureRecognizer *tap; to header file and load it in viewDidLoad, bit it doesn;t work well, it doesn't work on every cell. Rob, do you have skype? I could really use help with this, I am stuck on it for 2 days. – 1337code Dec 10 '12 at 20:28
  • @MarkM, It is assigned to IBOutlet, but only works on first 3 cells, I will try to create it in code – 1337code Dec 10 '12 at 20:29

3 Answers3

1
  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? Let's first make sure your UIActivityIndicatorView is working correctly. If it works when you comment out setImageWithURL, then the problem clearly rests there. If it doesn't work, even when you disable setImageWithURL, then you have a more fundamental issue. 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.

  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 (e.g. invoking cell = [tableView cellForRowAtIndexPath:indexPath] and confirm a non-nil value) because I'm assuming your setImageWithURL is operating asynchronously. (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. 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.)

  3. Unrelated, but I wouldn't be adding the UITapGestureRecognizer every time. 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. 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).

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

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;
}
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
0
NINetworkImageView *imageView=[[NINetworkImageView alloc]init];
imageView.delegate=self;
[imageView setPathToNetworkImage:[NSString stringWithStdString:imageUrl]];

Use a third party class to solve this task.

Pang
  • 9,564
  • 146
  • 81
  • 122