views:

3254

answers:

2

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..!

A: 

I think the cell's imageView will probably have a zero-size rectangle, because you haven't put a picture in it. So the spinner is inside but invisible.

So instead of putting the spinner within the imageView, just put it within the cell...

[cell addSubview:spinner];
spinner.frame = CGRectMake(145, 0, 30, 30); // if you want it centered horizontally...

you could also do

cell.accessoryView = spinner;

to put the spinner over at the far right of the cell.

David Maymudes
The acessoryView worked very well... But the spinner.frame doesn´t... I found a solution that is to create a "white square" UIImage and them put as cell.imageView.image... Them the spinner appears... But thanks for your help..!
Thiago
sorry about that; I should have tested it myself rather than trusting the other commenter!
David Maymudes
+4  A: 

I found the answer...

David Maymudes was partialy right... It´s necessary to have a "background" to the cell.imageView... But must be a image, not just a frame. So just create a UIImage as a "white background" and set in cell.imageView.image. The code will be:

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

The whiteback.png is just a 25x25 pixels white square...

Thanks for everyone help... See you...

Thiago