2010-04-19 121 views
0

我在NSOperation中遇到問題。我嘗試了很多方法,但它會在屏幕後面運行,但不會顯示在我的桌面視圖中。任何人都可以幫助我解決這個問題。我是NSOperation的新手。NSOperation無法在TableView上加載數據

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 
    [email protected]"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]; 

} 

回答

3

你還會初始化pics(即NSMutableArray *pics = [[NSMutableArray alloc] init]或類似的東西)?如果是的話,我在代碼中找不到它。如果你不是,那可能是問題 - 你正在發送所有這些消息來將對象添加到數組中nil