iphone

iPhone - initWithFrame not working for UIActivityIndicatorView?


I am using a tableview with custom cells and want to change the content of the tableview on a button click. I am using an activity indicator while the cells are loading data.

This is how I am creating the indicatorview

indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(141.0, 190.0, 37.0, 37.0)];
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
indicator.hidesWhenStopped = YES;
[[self tableView] addSubview:indicator];

But it appears as if the frame has no effect on the indicator. The indicator instead of displaying in the center of the table, appears in the navigation bar on the top left corner of the view.

Why is this happening?

alt text http://www.freeimagehosting.net/uploads/95cbe49850.png


Solution

  • initWithFrame: is not the suggested method for creating a UIActivityInidcator. Taking a look at the API, you can see that to create a UIActivityIndicator, you should use

    [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    

    Then add it to your view and set its position with setCenter: as suggested by Pablo.

    Also, adding it to a location in a table view will make it scroll with the table if the user scrolls up or down. If you put it in the table's superview instead, it will stay in place.