加载数据时无法显示 UIActivityIndi​​catorView

发布于 2024-11-25 11:06:01 字数 1777 浏览 5 评论 0原文

我在 TableViewController 上有一个 UIActivityIndi​​catorView 。当我在 init 中开始动画时,我将看到活动指示器,但是当我将启动命令移到其他位置时,它将无法工作/显示。有谁知道我做错了什么,我似乎无法在这里找到它。我希望它在加载数据时显示。

TableViewController.h

#import <UIKit/UIKit.h>


@interface CategoryTableViewController : UITableViewController {
    NSArray *cats;
    UIActivityIndicatorView *activityIndicator;
}

@end

TableViewController.m

#import "CategoryTableViewController.h"
#import "NieuwsDataManager.h"
#import "CategoryTableCell.h"
#import "NiewsCategory.h"
#import "NieuwsTableViewController.h"


@implementation CategoryTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        cats = [[NSArray alloc ] initWithArray:[NieuwsDataManager sharedInstance].newsCategories];
        self.title = @"Nieuws" ;
        activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(100,150,120,120)];
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;


        [self.view addSubview:activityIndicator];


    }
    return self;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [activityIndicator startAnimating];



    NiewsCategory *cat = [[NieuwsDataManager sharedInstance].newsData objectForKey:[cats objectAtIndex:indexPath.row]];



    [[NieuwsDataManager sharedInstance] getNewsByCategory:cat];



    NieuwsTableViewController *nt = [[NieuwsTableViewController alloc] initWithCategory:cat];

    [activityIndicator stopAnimating];

    [self.navigationController pushViewController:nt animated:YES];

    [nt release];

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

I have an UIActivityIndicatorView on a TableViewController. When I start animating in the init I will see the activity indicator, but when I move the start command anywhere else it will not work/show. Does anyone have an idea what I am doing wrong, I cannot seem to find it on here. I want it to show while I am loading data.

TableViewController.h

#import <UIKit/UIKit.h>


@interface CategoryTableViewController : UITableViewController {
    NSArray *cats;
    UIActivityIndicatorView *activityIndicator;
}

@end

TableViewController.m

#import "CategoryTableViewController.h"
#import "NieuwsDataManager.h"
#import "CategoryTableCell.h"
#import "NiewsCategory.h"
#import "NieuwsTableViewController.h"


@implementation CategoryTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        cats = [[NSArray alloc ] initWithArray:[NieuwsDataManager sharedInstance].newsCategories];
        self.title = @"Nieuws" ;
        activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(100,150,120,120)];
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;


        [self.view addSubview:activityIndicator];


    }
    return self;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [activityIndicator startAnimating];



    NiewsCategory *cat = [[NieuwsDataManager sharedInstance].newsData objectForKey:[cats objectAtIndex:indexPath.row]];



    [[NieuwsDataManager sharedInstance] getNewsByCategory:cat];



    NieuwsTableViewController *nt = [[NieuwsTableViewController alloc] initWithCategory:cat];

    [activityIndicator stopAnimating];

    [self.navigationController pushViewController:nt animated:YES];

    [nt release];

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩心不撩汉 2024-12-02 11:06:01
[self.view addSubview:activityIndicator];

从这段代码中,我认为 tableView 可能没有像 self.view 那样设置。您将其添加到 view 中,因此您的方法 [activityIndi​​cator startAnimating]; 可能实际上正在工作,但您必须将其设置为“将子视图置于前面”。 ”它可能在桌面视图下方进行动画处理。

[self.view addSubview:activityIndicator];

From this code I think it's possible that tableView is not as set as the self.view. You are adding it in the view so it might be possible that your method [activityIndicator startAnimating];is actually working, but you have to set it "bring subview to front." It's possible that it is animated underneath the tableview.

绿萝 2024-12-02 11:06:01

我会这样做:

- (id)initWithStyle:(UITableViewStyle)style {
  //your code
  [self.view addSubview:activityIndicator];
  [activityIndicator startAnimating];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidLoad];
  [activityIndicator stopAnimating];
}

我找到了一个关于此的教程,它展示了一种非常好的方法来做到这一点:
http:// www.markbetz.net/2010/09/30/ios-diary-showing-an-activity-spinner-over-a-uitableview/

I would do this:

- (id)initWithStyle:(UITableViewStyle)style {
  //your code
  [self.view addSubview:activityIndicator];
  [activityIndicator startAnimating];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidLoad];
  [activityIndicator stopAnimating];
}

And i found a tutorial about this, it shows a really nice way to do this:
http://www.markbetz.net/2010/09/30/ios-diary-showing-an-activity-spinner-over-a-uitableview/

别在捏我脸啦 2024-12-02 11:06:01

我过去注意到 UIActivityIndi​​cators 确实在 UI 线程挂起时进行绘制,但您可能需要在后台线程中执行任务,以便 UI 线程为进度指示器设置动画。请参阅 Apple 关于并发编程的文档。一般来说,最好在后台线程上执行冗长的操作,以便 UI 线程可以保持对用户的响应。

I have noticed in the past that UIActivityIndicators do draw while the UI thread is hung, but it may be possible that you need to perform your task in a background thread in order for the UI thread to animate the progress indicator. See Apple's documentation about Concurrency Programming. In general it is good to perform lengthy operations on a background thread so that the UI thread can remain responsive to the user.

油饼 2024-12-02 11:06:01

self.view 是否已在 init 方法中可用?这样的操作不应该在 viewDidLoad 方法中执行吗?

Is self.view already available in init method? Shouldn't such operation performed in viewDidLoad method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文