来源:http://www.codeios.com/thread-149-1-1.html
在之前iPhone利用XML传递 数据在Table界面中展示,介绍了利用xml加载数据,并且分析树形结构,最后把数据展示在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
其中tableview,利用IB和相应的代码相连接。
本文详细介绍了如何使用Objective-C语言通过XML加载数据,解析XML树形结构,并将数据动态地添加到Table列表中进行展示。重点在于实现过程中的线程操作、数据解析与列表更新。
10万+

被折叠的 条评论
为什么被折叠?



