Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPhone SDK: Adding a UIActivityIndicatorView to a UITableViewCell

Why doesn't the cell show anything in this code:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell.imageView addSubview:spinner];
[spinner startAnimating];
cell.textLabel.text=@"Carregando...";
[spinner release];

I'm doing this inside tableView:cellForRowAtIndexPath:.

I tried size to fit, create a frame to cell.imageView and a same size frame to the spinner, but nothing works.

What´s wrong with this code?

Thank you..!

like image 544
Thiago Avatar asked Aug 12 '09 22:08

Thiago


1 Answers

A slight modification of the method from Thiago:

I think adding the spinner directly to the cell view felt a little hacky, so I used a 1x1 transparent png as the image view and resized it to be whatever my spinner size is:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"NoReuse"] autorelease];
cell.textLabel.text = @"Loading...";

UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] 
    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];

// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:@"spacer"];

UIGraphicsBeginImageContext(spinner.frame.size);

[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];

return cell;

That gives a cell that looks like this:

loading cell

like image 103
Chris R Avatar answered Sep 27 '22 21:09

Chris R