NSOperation 无法在 TableView 上加载数据
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否曾经初始化
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 tonil
.