Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 加载UITableViewCell时如何设置UIActivityIndicatorView_Iphone_Objective C_Ios_Xcode_Uiactivityindicatorview - Fatal编程技术网

Iphone 加载UITableViewCell时如何设置UIActivityIndicatorView

Iphone 加载UITableViewCell时如何设置UIActivityIndicatorView,iphone,objective-c,ios,xcode,uiactivityindicatorview,Iphone,Objective C,Ios,Xcode,Uiactivityindicatorview,我有两个UITableViewControllers,A和B。当我点击表A中的一个单元格时,我将使用UINavigationController推送表视图控制器B。但是B的数据是从Internet下载的,这需要几秒钟。因此,我想在加载B时添加一个UIActivityIndicatorView。如何实现这一点?您可以将UIActivityIndicatorView添加为单元格的附件视图 - (void)tableView:(UITableView *)tableView didSelectRowA

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

您可以将UIActivityIndicatorView添加为单元格的
附件视图

- (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];
}

在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()
activityIndicator.stopAnimating()
现在调用从网络下载数据的方法

myDownloadMethod()
如果您不希望UI在此过程中没有响应,请在不同的线程中执行此操作

读这篇文章。

当您收到已下载内容的通知时,停止指示器

// 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()
activityIndicator.stopAnimating()

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

这对我来说很好,您可以尝试:

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
}
在索引路径中单击HighlightRowAtIndexPath时调用
[activityIndicator startAnimating]
, 当
取消高亮RowatineXpath
取消选择RowatineXpath
有用时,调用
[activityIndicator Stop Animating]

- (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];
}

如果服务器上有更多可用数据,下面的代码将在表视图的页脚显示微调器。您可以根据从服务器获取数据的逻辑对其进行更改

- (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;
}

你试过了吗?它根本不起作用。。。你能给我一个示例项目吗?问题是在屏幕中央显示一个大微调器,而不是在每个单元格中。尝试将[spinner startAnimating]添加到上面的示例中:它应该允许你在表a的附件视图中放置一个微调器,直到表B准备好加载为止,如果这是你想要的。OMG!我真的忘了加上它了。谢谢蒂姆·迪恩。@Wu Linson,对不起,我忘了添加重要的一行。您应该添加[spinner startAnimating];看我更新的答案。你能给我更多的细节吗?