iphone利用线程实现数据的加载,并展示在table列表中

实现的过程是修改两个table 的controller类,修改方法如下:
#import <UIKit/UIKit.h>
@interface WelcomePavilionViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
{
	NSMutableArray  *array;
	IBOutlet UITableView *tableView;
}
@property (nonatomic,retain) NSMutableArray  *array;
@property (nonatomic,retain) UITableView *tableView;
@end
实现方法是:
#import “WelcomePavilionViewController.h”
#import “XmlWelcome.h”
@implementation WelcomePavilionViewController
@synthesize array,tableView;
- (void)viewDidLoad {
	[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
	if ([self.array count]==0) {
		[NSThread detachNewThreadSelector:@selector(myTaskMethod) toTarget:self withObject:nil];
	}
}
-(void)myTaskMethod
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	XmlWelcome *parser=[[XmlWelcome alloc]
	initWithContentsOfURL:[NSURL URLWithString:@"http://mp.myvsp.cn/welcomedemos/getpavilionxml.json?area=a&width=80&height=80&digest_length=20" ]];
	//设置代理
	[parser setDelegate:parser];
	[parser parse];
	self.array=parser.ones;
	[self.tableView reloadData];
	[parser  release];
	[pool release];

}
- (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 {
	self.array=nil;
	self.tableView=nil;
}
- (void)dealloc {
	[self.tableView release];
	[self.array release];
	[super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView
	numberOfRowsInSection:(NSInteger)section {
	return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];
	if (cell==nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
		reuseIdentifier:@”tag”] autorelease];
	}
	//表格设计
	NSDictionary* one = [array objectAtIndex:indexPath.row];
	cell.textLabel.text = [one objectForKey:@"title"];
	cell.detailTextLabel.text = [one objectForKey:@"content"];
	id path = [one objectForKey:@"image"];
	NSURL *url = [NSURL URLWithString:path];
	NSData *data = [NSData dataWithContentsOfURL:url];
	UIImage *image = [[UIImage alloc] initWithData:data cache:NO];
	cell.image=image;
	[image release];
	return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return @”Hobby Information:”;
}
@end

参考资料:http://wangjun.easymorse.com/?p=881

这里总结一下,关键的总结点是这里

- (void)viewWillAppear:(BOOL)animated {
	if ([self.array count]==0) {
		[NSThread detachNewThreadSelector:@selector(myTaskMethod) toTarget:self withObject:nil];
	}
}
然后再在自己的调用方法里面调用 [self.tableView reloadData];这个方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值