`
jsntghf
  • 浏览: 2476787 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

NSInvocationOperation的使用

    博客分类:
  • iOS
阅读更多

以前写过一篇介绍NSInvocationOperation的文章:多线程之NSInvocationOperation,这篇文章是基于此的一个例子。

 

RootViewController.h

 

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
	NSMutableArray *array;
}

@property (nonatomic, retain) NSMutableArray *array;

@end

 

RootViewController.m

 

#import "RootViewController.h"

@implementation RootViewController
@synthesize array;

- (void)viewDidLoad {
    [super viewDidLoad];
    
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Load" 
                                                                              style:UIBarButtonItemStyleDone 
                                                                             target:self 
                                                                             action:@selector(loadData)];
	
	NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000];
	self.array = _array;
	[_array release];	
}

- (void) loadData {	
	NSOperationQueue *queue = [NSOperationQueue new];
	NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                            selector:@selector(loadDataWithOperation) object:nil];
	[queue addOperation:operation];
	[operation release];
}

- (void) loadDataWithOperation {
	NSURL *dataURL = [NSURL URLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"];
	
	NSArray *tmp_array = [NSArray arrayWithContentsOfURL:dataURL];
	
	for(NSString *str in tmp_array) {
		[self.array addObject:str];
	}
	
	[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	[cell.textLabel setText:[self.array objectAtIndex:indexPath.row]];
	
    return cell;
}

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

@end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics