How can i add a activity indicator below the UITableView?

Emon picture Emon · Oct 31, 2011 · Viewed 23.6k times · Source

In my application i want to add activity indicator under UItableview where tableview will scroll but i do not know how can i add activity indicator over there.

To elaborate,when i will finish the scrolling of tableview then for more data i have to set a refresh option by an activity indicator.

i have tried it at the top of the tableview and it worked but i dont know how can i add it below the tableview. here is some sample code..

      - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (isLoading) return;
isDragging = YES;


refreshHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshHeaderView.backgroundColor = [UIColor clearColor];

refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshLabel.backgroundColor = [UIColor clearColor];
refreshLabel.font = [UIFont boldSystemFontOfSize:12.0];
refreshLabel.textAlignment = UITextAlignmentCenter;

refreshArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresharrow.png"]];
refreshArrow.frame = CGRectMake((scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 27) / 2,
                                (scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 44) / 2,
                                27, 44);

refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
refreshSpinner.frame = CGRectMake((scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 20) / 2, (scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 20) / 2, 20, 20);
refreshSpinner.hidesWhenStopped = YES;

[refreshHeaderView addSubview:refreshLabel];
[refreshHeaderView addSubview:refreshArrow];
[refreshHeaderView addSubview:refreshSpinner];
[tableview addSubview:refreshHeaderView];


}

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (isLoading) {
    // Update the content inset, good for section headers
    if (scrollView.contentOffset.y > 0){
        NSLog(@"scrollView.contentOffset.y 1= %d",scrollView.contentOffset.y );
        tableview.contentInset = UIEdgeInsetsZero;
    }
    else if (scrollView.contentOffset.y >= -REFRESH_HEADER_HEIGHT){
        NSLog(@"scrollView.contentOffset.y 2= %d",scrollView.contentOffset.y );
        tableview.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
} else if (isDragging && scrollView.contentOffset.y > scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT) {
    // Update the arrow direction and label
    [UIView beginAnimations:nil context:NULL];
    if (scrollView.contentOffset.y > scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT) {
        // User is scrolling above the header
        NSLog(@"scrollView.contentOffset.y 3= %d",scrollView.contentOffset.y );
        refreshLabel.text = self.textRelease;
        [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
    } else { // User is scrolling somewhere within the header
        refreshLabel.text = self.textPull;
        [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
    }
    [UIView commitAnimations];
}
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (isLoading) return;
isDragging = NO;
if (scrollView.contentOffset.y >= scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT) {
    // Released above the header
    [self startLoading];
}
}

so please someone give me some example code about how can i do that.

actually i am new in iphone application development.So please help me.

Thanks in Advance.

Answer

kaka picture kaka · Dec 31, 2011

This is how I've done it:

UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
[spinner startAnimating];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.tableView.tableFooterView = spinner;

Then you just set tableFooterView to nil to remove it.

Note: 44, btw, is the default height of a UITableViewCell when using UITableViewStylePlain. It's 45 for UITableViewStyleGrouped.