实现的过程是修改两个table 的controller类,修改方法如下:
1 2 3 4 5 6 7 8 9 10 #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
实现方法是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #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 { [super didReceiveMemoryWarning]; } - (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
这里总结一下,关键的总结点是这里
1 2 3 4 5 - (void )viewWillAppear:(BOOL )animated { if ([self .array count]==0 ) { [NSThread detachNewThreadSelector:@selector (myTaskMethod) toTarget:self withObject:nil ]; } }
然后再在自己的调用方法里面调用**[self.tableView reloadData];
这个方法**