views:

229

answers:

3

Hi everyone, I want to do something pretty simple with my UITableView: I want to add a UIActivityIndicatorView to a section's header view, and make it animate or disappear whenever I want.

I had no trouble adding the UIActivityIndicatorView to the header view using tableView:viewForHeaderInSection:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];

// create the title
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 12.0, 310.0, 22.0)];
headerLabel.text = @"some random title here";

[customView addSubview:headerLabel];
[headerLabel release];

// Add a UIActivityIndicatorView in section 1
if(section == 1)
{
    [activityIndicator startAnimating];
    [customView addSubview:activityIndicator];
}

return [customView autorelease];

}

activityIndicator is a property of my controller's class. I alloc it in the viewDidLoad method:

- (void)viewDidLoad 
{ 
(...)
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(200, 10, 25, 25)];
}

This way I can send messages to it (like -startAnimating or -stopAnimating) whenever I want. The problem is that the activityIndicator disappears as soon as I scroll the tableView (I guess it is because the tableView:viewForHeaderInSection: method is called a second time).

How else can I add an activityIndicatorView to the section's header view and still be able to send messages to it afterwards? (with the activityIndicator not disapearing when I scroll down of course)

Thank you very much!

A: 

If you try to use the same activity indicator in multiple places then it is probably getting moved from one place to the other. I believe you need a different one for every single section header. You might want to use a MutableArray to keep track of the header views you create so you can reuse them if you find one in the array that doesn't have a superview, sort of like dequeuing and reusing cells.

This is just a guess as I haven't done this, but I'm pretty sure the issue is trying to reuse the same view in multiple places.

Nimrod
I don't want to have an activityIndicator in multiple places, just in one.
Supernico
Well, that's what it looked like you were trying to do since I couldn't imagine any other reason to keep adding the subview to the same
Nimrod
A: 

The problem seemed to be caused by re-creating a customView and adding the activityIndicator as a subview every time tableView:viewForHeaderInSection: is called.

Not using subviews helped me fix it:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

// Add a UIActivityIndicatorView in section 1
if(section == 1)
{
    [activityIndicator startAnimating];
    return activityIndicator;
}

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];

// create the title
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 12.0, 310.0, 22.0)];
headerLabel.text = @"some random title here";

[customView addSubview:headerLabel];
[headerLabel release];


return [customView autorelease];
}

(it looks quite ugly though, the activityIndicator takes the whole width of the section. I'd better create a unique customView for section 1 and add the activityIndicator as a subView once and for all).

Supernico
A: 

is that activityIndicator an IBoutlet??? actally i cant even drag activity indicator to my tableview

prajakta
no it's not. I didn't use Interface Builder for this.
Supernico