Replace UIImageView with UIActivityIndicatorView when loading images in a UITableViewCell

I have a UITableViewCell that has 2 UIImageView defined in an xib file. Now I came up with the idea to decorate ui, and I would like to change the runtime of UIImageView with UIActivityIndicatorView until the asynchronous response returns.

Unfortunately, UIActivityIndicatorView does not display it at all, just the full image when it is finished.

Here is the piece of code that is relevant:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myTableCellId]; cell.selectionStyle = UITableViewCellSelectionStyleNone; 

...

  UIImageView *imageView = (UIImageView *)[cell viewWithTag:200]; 

...

  imageView.image=nil; // reset image as it will be retrieved asychronously UIActivityIndicatorView *loadingActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; [loadingActivity startAnimating]; loadingActivity.frame = imageView.frame; UIView* parent = [imageView superview]; [parent addSubview:loadingActivity]; [parent bringSubviewToFront:loadingActivity]; [myObject callMyFunction:index completionBlock:^(UIImage *img) { dispatch_async(dispatch_get_main_queue(), ^{ imageView.image=img; [loadingActivity stopAnimating]; [loadingActivity removeFromSuperview]; //DLog(@"Got image for: %@", titleLabel.text); }); }]; return cell; } 

Any ideas what is wrong?

+2
source share
2 answers

If you have a white counter on a white background, it will not be displayed.

+3
source

Set the frame and then start the animation. Also try changing the frame that you assigned to it.

 loadingActivity.frame = CGRectMake(20, 20, 40, 20) 

You can also set the property loadingActivity.hidesWhenStopped = YES; instead of running removeFromSuperview.

+1
source

Source: https://habr.com/ru/post/1495609/


All Articles