在UITableView底部添加活动指示器?

8
我有一个带分页的UITableview,首先从服务器获取20个对象并在UITableView中填充,当到达最后一行时需要再次调用服务以获取下一个20个对象。
我的问题是我需要在表格底部添加活动指示器,并应该显示“加载中”,用户可以向上滚动查看当前对象,但不应向下滚动。
是否有自定义控件? 是否有更好的方法来实现?
提前致谢。

你为什么不使用设置了适当帧的UIActivityIndicatorView呢?还有一些自定义控件,比如MBProgressHUD,但是你的应用程序似乎足够简单。 - user1349663
2个回答

26

让我们尝试使用 TableView Footer View 显示活动指示器。

例如:

在 .h 文件中声明 UIView * footerView;

在 .m 文件中添加以下方法

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     [self initFooterView];
 }

 -(void)initFooterView
 {
    footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];

    UIActivityIndicatorView * actInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    actInd.tag = 10;

    actInd.frame = CGRectMake(150.0, 5.0, 20.0, 20.0);

    actInd.hidesWhenStopped = YES;

    [footerView addSubview:actInd];

    actInd = nil;
 }

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
     BOOL endOfTable = (scrollView.contentOffset.y >= ((self.contentArray.count * 40) - scrollView.frame.size.height)); // Here 40 is row height

    if (self.hasMoreData && endOfTable && !self.isLoading && !scrollView.dragging && !scrollView.decelerating)
   {
        self.tableView.tableFooterView = footerView;

        [(UIActivityIndicatorView *)[footerView viewWithTag:10] startAnimating];
   }

}

谢谢!


谢谢朋友,让我试一下 @Natarajan - Sugan S
2
这是我迄今为止看到的最佳解决方案。你很棒,添加了有用的答案,而不像那些对这个问题进行贬低批评的人一样。 - HotFudgeSunday

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接