iphone Dev - 活动指示器,NSThread无法在Nav控制器表视图上工作

时间:2021-11-08 11:11:37

I really can't get this to work, basically when my JSON feeds loads I want the indicator to show, then hide when it's stopped.

我真的无法让这个工作,基本上当我的JSON提供加载我希望指示器显示,然后隐藏它停止时。

It loads top level menu items 1st "Publishing, Broadcasting, Marketing Services", then when Broadcasting is selected it loads a feed using the JSON framework hosted on Google. Round this load I call startIndicator and stopIndicator using the NSThread. Have I missed something?

它加载*菜单项第一个“发布,广播,营销服务”,然后选择广播时,它使用托管在Google上的JSON框架加载一个订阅源。围绕此负载我使用NSThread调用startIndicator和stopIndicator。我错过了什么吗?

@implementation GeneralNewsTableViewController

@synthesize dataList;
@synthesize generalNewsDetailViewController;
@synthesize atLevel;


-(void) startIndicator
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc ] init ];
 [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
 [pool release];

}

-(void) stopIndicator
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc ] init ];
 [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating];
 [pool release];

}

- (void)viewDidLoad {

 NSMutableArray *checker = self.dataList;

 if(checker == nil)
 {
  self.title = NSLocalizedString(@"General1",@"General News");

  NSMutableArray *array = [[NSArray alloc] initWithObjects:@"Publishing", @"Broadcasting",@"Marketing Services",nil];
  self.dataList = [array retain];
  self.atLevel = @"level1";
  [array release];
 }

  UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
  //set the initial property
  [activityIndicator stopAnimating];
  [activityIndicator hidesWhenStopped];
  //Create an instance of Bar button item with custome view which is of activity indicator
  UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
  //Set the bar button the navigation bar
  [self navigationItem].rightBarButtonItem = barButton;
  //Memory clean up
  [activityIndicator release];
  [barButton release];


 [super viewDidLoad];
}



 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSString *level = self.atLevel;
  if([level isEqualToString:@"level2"])
  {
   return 70.0f;
  }
  else
  {
   return 40.0f;

  }
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *FirstLevelCell = @"FirstLevelCell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
 if(cell == nil)
 {
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:FirstLevelCell] autorelease];
 }

 NSInteger row = [indexPath row]; 

 NSString *level = self.atLevel;
 if([level isEqualToString:@"level2"])
 { 
  NSMutableArray *stream = [self.dataList objectAtIndex:row];
  NSString *newsTitle = [stream valueForKey:@"title"];

  if( ![newsTitle isKindOfClass:[NSString class]] )
  {
   cell.textLabel.text =  @"";
  }
  else 
  {
   cell.textLabel.text = [stream valueForKey:@"title"]; 
  }

   cell.textLabel.numberOfLines = 2;
   cell.textLabel.font =[UIFont systemFontOfSize:10];

   cell.detailTextLabel.numberOfLines = 1;
   cell.detailTextLabel.font= [UIFont systemFontOfSize:8];
   cell.detailTextLabel.text = [stream valueForKey:@"created"];


   NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.website.co.uk/images/stories/img.jpg"]];
   UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];

   cell.imageView.image = newsImage;

   [imageURL release];
   [newsImage release];
 }
 else 
 {
  cell.textLabel.text = [dataList objectAtIndex:row];
 }

 return cell;
}



- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSUInteger row = [indexPath row]; 
 NSMutableString *levelType = (NSMutableString *) [dataList objectAtIndex:row];

 if(![levelType isKindOfClass:[NSString class]])
 {
  if(self.generalNewsDetailViewController == nil)
  {
   GeneralNewsDetailViewController *generalDetail = [[GeneralNewsDetailViewController alloc] initWithNibName:@"GeneralNewsDetailView" bundle:nil];
   self.generalNewsDetailViewController = generalDetail;
   [generalDetail release];
  }

  NSDictionary *stream = [self.dataList objectAtIndex:row];

  NSString *newsTitle = [stream valueForKey:@"title"];

  if( ![newsTitle isKindOfClass:[NSString class]] )
  {
   generalNewsDetailViewController.newsTitle =  @"";
  }
  else 
  {
   generalNewsDetailViewController.newsTitle =[stream valueForKey:@"title"]; 
  }

  generalNewsDetailViewController.newsId = [stream valueForKey:@"id"];
  generalNewsDetailViewController.fullText = [stream valueForKey:@"fulltext"];
  generalNewsDetailViewController.newsImage = [stream valueForKey:@"images"];
  generalNewsDetailViewController.created = [stream valueForKey:@"created"];

  HowDo_v1AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
  [delegate.generalNewsNavController pushViewController:self.generalNewsDetailViewController animated:YES];

 }
 else
 {

  GeneralNewsTableViewController
   *generalSubDetail = [[GeneralNewsTableViewController alloc] initWithNibName:@"GeneralNewsTableView" bundle:nil];

  NSMutableArray *array;
  NSString *titleSelected = (NSString *) [dataList objectAtIndex:row];

  if([titleSelected isEqualToString:@"Publishing"])
  {
   generalSubDetail.title = @"Publishing news detail";
   array = [[NSArray alloc] initWithObjects:@"pub News1", @"pub News2",@"pub News3",nil];
   generalSubDetail.atLevel = @"level1";
  }
  else if ([titleSelected isEqualToString:@"Broadcasting"])
  {
   generalSubDetail.title = @"Broadcasting news detail";

  /// START  
 [self performSelectorOnMainThread:@selector(startIndicator) withObject:nil waitUntilDone:YES];

   if(jSONDataAccessWrapper == nil)
   {
    jSONDataAccessWrapper = [JSON_DataAccess_Wrapper alloc];
   } 
   array = [jSONDataAccessWrapper downloadJSONFeed];



 [self performSelectorOnMainThread:@selector(stopIndicator) withObject:nil waitUntilDone:YES];


   generalSubDetail.atLevel = @"level2";
  }
  else if ([titleSelected isEqualToString:@"Marketing Services"])
  {
   generalSubDetail.title = @"Marketing Services news detail";
   array = [[NSArray alloc] initWithObjects:@"Marketing News1", @"Marketing News2",@"Marketing News3",nil];
   generalSubDetail.atLevel = @"level1";
  }
  generalSubDetail.dataList = array;

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

  [titleSelected release];

 } 


}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
//- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSIndexPath *) section
{
 return [self.dataList count]; 
}

Cheers for any feedback

欢呼任何反馈

Frames

框架

1 个解决方案

#1


0  

It is usually recommended by Apple that operations on user interface should be done on the main thread.

Apple通常建议用户界面上的操作应该在主线程上完成。

What you can do is to defer the callback into the main thread:

你可以做的是将回调推迟到主线程:

- (void)startIndicator
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self performSelectorOnMainThread:@selector(startAnimating:) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void)startAnimating:(id)sender
{
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
}

Check out the Thread Guide from Apple. It contains useful information.

查看Apple的Thread Guide。它包含有用的信息。

#1


0  

It is usually recommended by Apple that operations on user interface should be done on the main thread.

Apple通常建议用户界面上的操作应该在主线程上完成。

What you can do is to defer the callback into the main thread:

你可以做的是将回调推迟到主线程:

- (void)startIndicator
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self performSelectorOnMainThread:@selector(startAnimating:) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void)startAnimating:(id)sender
{
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
}

Check out the Thread Guide from Apple. It contains useful information.

查看Apple的Thread Guide。它包含有用的信息。