How can i add a activity indicator below the UITableView?

廉价感情. 提交于 2019-11-30 03:48:43

The best way to add activity indicator is at footer of tableview.

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
UIView *headerView = [[[UIView alloc] init]autorelease];

    [headerView setBackgroundColor:[UIColor clearColor]];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Load More" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 210.0, 160.0, 40.0);


    [headerView addSubview:button];

    [headerLabel release];

    return headerView;

}  

Add a button to the footerView and set action to button (as you needed). This how app store tableview works. On clicking button fetch some more data add to table array scroll the table without animation.

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.

Chathuranga Silva

Swift Update :-

let pagingSpinner = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
pagingSpinner.startAnimating()
pagingSpinner.color = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
pagingSpinner.hidesWhenStopped = true
tableView.tableFooterView = pagingSpinner

One more way to add activity indicator. Check if your data displayed is not equals to the total count of data. Then you add one extra cell and Add "View More" button. When i click on the view more the hide that button and add activity indicator in that cell. And when process is done then reload the table view.

Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!