如何在tableView之前添加“正在加载”视图控制器

use*_*762 0 objective-c uitableview uiactivityindicatorview ios

我希望在tableView加载之前有一个“正在加载”视图控制器。您可以推荐执行此操作的最佳方法吗?如果可能,请提供一些代码。

理想情况下,加载屏幕将具有活动指示和标有加载的标签。

在UITableView上方添加一个UIView。

在此处输入图片说明

Rya*_*anG 5

首先在您的.h中定义它们

@property (strong, nonatomic) UIView *loadingView;
Run Code Online (Sandbox Code Playgroud)

然后在中viewWillAppear,在开始加载表视图数据之前,制作并显示视图:

if(!_loadingView)
{
    _loadingView = [[UIView alloc] initWithFrame:self.view.frame];
    [loadingView setBackgroundColor:[UIColor lightGrayColor]];

    UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [actInd setFrame:CGRectMake((loadingView.frame.size.width / 2 - 10), (loadingView.frame.size.height / 2 - 10), 20, 20)];

    UILabel *lblLoading = [[UILabel alloc] initWithFrame:CGRectMake((_loadingView.frame.size.width / 2 - 30), (_loadingView.frame.size.height / 2 - 5), 60, 30)];
    lblLoading.text = @"Loading";

    // you will probably need to adjust those frame values to get it centered    

    [loadingView addSubview:actInd];
    [loadingView addSubview:lblLoading];

    [actInd startAnimating];
} 

[_yourTableView addSubview:_loadingView];
Run Code Online (Sandbox Code Playgroud)

然后,当您完成数据加载时:

[_loadingView removeFromSuperView];
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Storyboard ...,只需将a拖动UIView到您的上UIViewController,确保其位于上方UITableView(不需要是的子视图UITableView)。拖动UIActivityIndicatorViewUILabel上,然后创建了一个出口UIView

然后在你的 viewWillAppear

[_loadingView setHidden NO];
Run Code Online (Sandbox Code Playgroud)

然后,当您完成加载

[_loadingView setHidden YES];
Run Code Online (Sandbox Code Playgroud)