如何在加载UITableViewCell时设置UIActivityIndicatorView

39

我有两个UITableViewController,A和B。当我在A表格视图中点击一个单元格时,我将使用UINavigationController来推出表格视图控制器B。但是B的数据是从互联网下载的,这需要数秒钟的时间。因此,我想在加载B时添加一个UIActivityIndicatorView。我该如何实现这一点?

5个回答

53

您可以将UIActivityIndicatorView 添加为单元格的accessoryView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.frame = CGRectMake(0, 0, 24, 24);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = spinner;
    [spinner startAnimating];
    [spinner release];
}

1
你试过了吗?它完全不起作用... 能否给我一个样例项目? - Wu Linsen
4
问题是关于在屏幕中央显示一个大型旋转图标,而不是在每个单元格内显示。 - Akshay
1
尝试在上面的示例中添加[spinner startAnimating]:如果您希望在表B准备加载之前,在表A的附属视图中放置一个旋转器,则应该允许您这样做。 - Tim Dean
哎呀!我真的忘记添加它了。谢谢Tim Dean。 - EmptyStack
1
@吴林森,抱歉我忘记添加一个重要的行。你应该加上 [spinner startAnimating]; 请看我的更新答案。 - EmptyStack
真的很有效!为了将spinner放置在单元格中央,我将spinner.frame更改为spinner.frame =CGRectMake (cell.center.y, cell.center.x, cell.frame.size.width, cell.frame.size.height); - Sam

30

在tableview B类的viewDidLoad方法中,添加一个活动指示器。

// Create the Activity Indicator.
    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    activityIndicator.hidesWhenStopped = true
    view.addSubview(activityIndicator)

    // Position it at the center of the ViewController.
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)])
    activityIndicator.startAnimating()

现在调用你的从网络下载数据的方法。

myDownloadMethod()

如果您不想在过程中使用户界面失去响应,请在另一个线程中执行。

请参阅此线程。 可以使用后台线程解析数据吗?

当通知内容已下载时,请停止指示器。

activityIndicator.stopAnimating()

现在你可以调用 tableview.reloadData() 来重新加载表格以显示新内容。


你能给我更多的细节吗? - Wu Linsen

8
UIActivityIndicatorView * activityindicator1 = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(150, 200, 30, 30)];
[activityindicator1 setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityindicator1 setColor:[UIColor orangeColor]];
[self.view addSubview:activityindicator1];
[activityindicator1 startAnimating];

[self performSelector:@selector(callfunction) withObject:activityindicator1 afterDelay:1.0];

-(void)callfunction
{
// Here your stuf
}

6

这对我很有效,你可以尝试一下:

didHighlightRowAtIndexPath 时调用 [activityIndicator startAnimating],在 didUnhighlightRowAtIndexPath 时调用 [activityIndicator stopAnimating]didSelectRowAtIndexPath 更有用。

- (void)runIndicatorAtIndexPath:(NSIndexPath *)indexPath display:(BOOL)playing{ 
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = activityIndicator;
    playing == YES ?[activityIndicator startAnimating]:[activityIndicator stopAnimating];
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    [self runIndicatorAtIndexPath:indexPath display:YES];
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    [self runIndicatorAtIndexPath:indexPath display:NO];
}

4
这段代码将在表格视图的页脚显示一个旋转器,如果服务器上还有更多数据可用。您可以根据从服务器获取数据的逻辑进行更改。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     /* set cell attributes here */
     NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
     NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
     if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
         if(isMoreDataAvailableOnserver)
         {
             [self showSpinnerAtFooter];
             [self getMoreDataFromServer];
         }
         else {
             [self hideSpinnerAtFooter];
         }
     }
     return cell;
}

- (void)hideSpinnerAtFooter {
    self.tableView.tableFooterView = nil;
}

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

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