加载数据时无法显示 UIActivityIndicatorView
我在 TableViewController 上有一个 UIActivityIndicatorView 。当我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从这段代码中,我认为
tableView
可能没有像self.view
那样设置。您将其添加到view
中,因此您的方法[activityIndicator startAnimating];
可能实际上正在工作,但您必须将其设置为“将子视图置于前面”。 ”它可能在桌面视图下方进行动画处理。From this code I think it's possible that
tableView
is not as set as theself.view
. You are adding it in theview
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.我会这样做:
我找到了一个关于此的教程,它展示了一种非常好的方法来做到这一点:
http:// www.markbetz.net/2010/09/30/ios-diary-showing-an-activity-spinner-over-a-uitableview/
I would do this:
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/
我过去注意到 UIActivityIndicators 确实在 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.
self.view 是否已在 init 方法中可用?这样的操作不应该在 viewDidLoad 方法中执行吗?
Is
self.view
already available in init method? Shouldn't such operation performed inviewDidLoad
method?