NSOperation 无法在 TableView 上加载数据

发布于 2024-08-29 13:50:27 字数 4556 浏览 4 评论 0原文

我在 NSOperation 中遇到问题。我尝试了很多方法,但它会在屏幕后面运行,但不会使其出现在我的表格视图上。谁能帮我解决这个问题。我是 NSOperation 的新手。

最近记录.h

#import <UIKit/UIKit.h>
#import "FlickrFetcher.h"

@interface Recents : UITableViewController {
 FlickrFetcher *fetcher;
 NSString *name;
 NSData *picture;
 NSString *picName;
 NSMutableArray *names;
 NSMutableArray *pics;
 NSMutableArray *lists;
 NSArray *namelists;
 NSOperationQueue *operationQueue;
}
@property (nonatomic,retain)NSString *name;
@property (nonatomic,retain)NSString *picName;
@property (nonatomic,retain)NSData *picture;
@property (nonatomic,retain)NSMutableArray *names;
@property (nonatomic,retain)NSMutableArray *pics;
@property(nonatomic,retain)NSMutableArray *lists;
@property(nonatomic,retain)NSArray *namelists;
@end

最近记录.m

#import "Recents.h"
#import "PersonList.h"
#import "PhotoDetail.h"

@implementation Recents


@synthesize picName,picture,name,names,pics,lists,namelists;
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
  self.title=@"Recents";
    }
    return self;
}

- (void)beginLoadingFlickrData{

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousLoadFlickrData) object:nil];
    [operationQueue addOperation:operation];
    [operation release];
}

- (void)synchronousLoadFlickrData{
 fetcher=[FlickrFetcher sharedInstance];
 NSArray *recents=[fetcher recentGeoTaggedPhotos];
 [self performSelectorOnMainThread:@selector(didFinishLoadingFlickrDataWithResults:) withObject:recents waitUntilDone:NO];
}

- (void)didFinishLoadingFlickrDataWithResults:(NSArray *)recents{

 for(NSDictionary *dic in recents){
  [names addObject:[fetcher usernameForUserID:[dic objectForKey:@"owner"]]];
  if([[dic objectForKey:@"title"]isEqualToString:@""]){
   [pics addObject:@"Untitled"];
  }else{
   [pics addObject:[dic objectForKey:@"title"]];
  }
  NSLog(@"OK!!");
 }
 [self.tableView reloadData];
    [self.tableView flashScrollIndicators];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
 operationQueue = [[NSOperationQueue alloc] init];
 [operationQueue setMaxConcurrentOperationCount:1];
 [self beginLoadingFlickrData];
 self.tableView.rowHeight = 95;

}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 return[names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
 if (cell == nil) {
  cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
 }
 cell.detailTextLabel.text=[names objectAtIndex:indexPath.row];
 cell.textLabel.text=[pics objectAtIndex:indexPath.row];
 //UIImage *image=[UIImage imageWithData:[self.lists objectAtIndex:indexPath.row]];
 //cell.imageView.image=image;
 return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 fetcher=[[FlickrFetcher alloc]init];
 PhotoDetail *scroll=[[PhotoDetail alloc]initWithNibName:@"PhotoDetail" bundle:nil];
 scroll.titleName=[self.pics objectAtIndex:indexPath.row];
 scroll.picture = [UIImage imageWithData:[self.lists objectAtIndex:indexPath.row]];
 [self.navigationController pushViewController:scroll animated:YES];

}

I have a problem in NSOperation. I tried many ways but it would run behind the screen, but will not make it appear on the my table view. Can anyone help me out with this. I am new to NSOperation.

Recents.h

#import <UIKit/UIKit.h>
#import "FlickrFetcher.h"

@interface Recents : UITableViewController {
 FlickrFetcher *fetcher;
 NSString *name;
 NSData *picture;
 NSString *picName;
 NSMutableArray *names;
 NSMutableArray *pics;
 NSMutableArray *lists;
 NSArray *namelists;
 NSOperationQueue *operationQueue;
}
@property (nonatomic,retain)NSString *name;
@property (nonatomic,retain)NSString *picName;
@property (nonatomic,retain)NSData *picture;
@property (nonatomic,retain)NSMutableArray *names;
@property (nonatomic,retain)NSMutableArray *pics;
@property(nonatomic,retain)NSMutableArray *lists;
@property(nonatomic,retain)NSArray *namelists;
@end

Recents.m

#import "Recents.h"
#import "PersonList.h"
#import "PhotoDetail.h"

@implementation Recents


@synthesize picName,picture,name,names,pics,lists,namelists;
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
  self.title=@"Recents";
    }
    return self;
}

- (void)beginLoadingFlickrData{

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousLoadFlickrData) object:nil];
    [operationQueue addOperation:operation];
    [operation release];
}

- (void)synchronousLoadFlickrData{
 fetcher=[FlickrFetcher sharedInstance];
 NSArray *recents=[fetcher recentGeoTaggedPhotos];
 [self performSelectorOnMainThread:@selector(didFinishLoadingFlickrDataWithResults:) withObject:recents waitUntilDone:NO];
}

- (void)didFinishLoadingFlickrDataWithResults:(NSArray *)recents{

 for(NSDictionary *dic in recents){
  [names addObject:[fetcher usernameForUserID:[dic objectForKey:@"owner"]]];
  if([[dic objectForKey:@"title"]isEqualToString:@""]){
   [pics addObject:@"Untitled"];
  }else{
   [pics addObject:[dic objectForKey:@"title"]];
  }
  NSLog(@"OK!!");
 }
 [self.tableView reloadData];
    [self.tableView flashScrollIndicators];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
 operationQueue = [[NSOperationQueue alloc] init];
 [operationQueue setMaxConcurrentOperationCount:1];
 [self beginLoadingFlickrData];
 self.tableView.rowHeight = 95;

}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 return[names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
 if (cell == nil) {
  cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
 }
 cell.detailTextLabel.text=[names objectAtIndex:indexPath.row];
 cell.textLabel.text=[pics objectAtIndex:indexPath.row];
 //UIImage *image=[UIImage imageWithData:[self.lists objectAtIndex:indexPath.row]];
 //cell.imageView.image=image;
 return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 fetcher=[[FlickrFetcher alloc]init];
 PhotoDetail *scroll=[[PhotoDetail alloc]initWithNibName:@"PhotoDetail" bundle:nil];
 scroll.titleName=[self.pics objectAtIndex:indexPath.row];
 scroll.picture = [UIImage imageWithData:[self.lists objectAtIndex:indexPath.row]];
 [self.navigationController pushViewController:scroll animated:YES];

}

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

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

发布评论

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

评论(1

梦晓ヶ微光ヅ倾城 2024-09-05 13:50:27

您是否曾经初始化 pics (即 NSMutableArray *pics = [[NSMutableArray alloc] init] 或类似的东西)?如果是的话,我在代码中找不到它。如果不是,这可能就是问题所在——您正在发送所有这些消息以将对象添加到数组中以nil

Are you ever initializing pics (i.e., NSMutableArray *pics = [[NSMutableArray alloc] init] or something of the sort)? If you are, I can't find it in the code. If you aren't, that's probably the problem -- you're sending all of those messages to add objects to the array to nil.

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