NSOperation unable to load data on the TableView
- by yeohchan
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];
}