iPhone利用线程实现数据的加载并在Table中展示

本文详细介绍了如何使用Objective-C语言通过XML加载数据,解析XML树形结构,并将数据动态地添加到Table列表中进行展示。重点在于实现过程中的线程操作、数据解析与列表更新。

来源:http://www.codeios.com/thread-149-1-1.html

在之前iPhone利用XML传递 数据在Table界面中展示,介绍了利用xml加载数据,并且分析树形结构,最后把数据展示在table列表中。下面详细介绍一下怎么动态加载数据,最后添加到列表中。


实现的效果如下:

IMG_0066.png 

IMG_0067.png 


实现的过程是修改两个table 的controller类,修改方法如下:
  1. #import <UIKit/UIKit.h>

  2. @interface WelcomePavilionViewController : UIViewController
  3. <UITableViewDelegate,UITableViewDataSource>
  4. {
  5. NSMutableArray  *array;
  6. IBOutlet UITableView *tableView;
  7. }
  8. @property (nonatomic,retain) NSMutableArray  *array;
  9. @property (nonatomic,retain) UITableView *tableView;
  10. @end
复制代码


实现方法是:
  1. #import “WelcomePavilionViewController.h”
  2. #import “XmlWelcome.h”
  3. @implementation WelcomePavilionViewController
  4. @synthesize array,tableView;
  5. - (void)viewDidLoad {
  6. [super viewDidLoad];
  7. }
  8. - (void)viewWillAppear:(BOOL)animated {
  9. if ([self.array count]==0) {
  10. [NSThread detachNewThreadSelector:@selector(myTaskMethod) toTarget:self withObject:nil];
  11. }
  12. }
  13. -(void)myTaskMethod
  14. {
  15. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  16. XmlWelcome *parser=[[XmlWelcome alloc]
  17. initWithContentsOfURL:[NSURL URLWithString:@"http://mp.myvsp.cn/welcomedemos/getpavilionxml.json?area=a&width=80&height=80&digest_length=20" ]];
  18. //设置代理
  19. [parser setDelegate:parser];
  20. [parser parse];
  21. self.array=parser.ones;
  22. [self.tableView reloadData];
  23. [parser  release];
  24. [pool release];

  25. }
  26. - (void)didReceiveMemoryWarning {
  27. // Releases the view if it doesn’t have a superview.
  28. [super didReceiveMemoryWarning];
  29. // Release any cached data, images, etc that aren’t in use.
  30. }

  31. - (void)viewDidUnload {
  32. self.array=nil;
  33. self.tableView=nil;
  34. }
  35. - (void)dealloc {
  36. [self.tableView release];
  37. [self.array release];
  38. [super dealloc];
  39. }

  40. - (NSInteger)tableView:(UITableView *)tableView
  41. numberOfRowsInSection:(NSInteger)section {
  42. return [array count];
  43. }
  44. - (UITableViewCell *)tableView:(UITableView *)tableView
  45. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  46. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];
  47. if (cell==nil) {
  48. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  49. reuseIdentifier:@”tag”] autorelease];
  50. }
  51. //表格设计
  52. NSDictionary* one = [array objectAtIndex:indexPath.row];
  53. cell.textLabel.text = [one objectForKey:@"title"];
  54. cell.detailTextLabel.text = [one objectForKey:@"content"];
  55. id path = [one objectForKey:@"image"];
  56. NSURL *url = [NSURL URLWithString:path];
  57. NSData *data = [NSData dataWithContentsOfURL:url];
  58. UIImage *image = [[UIImage alloc] initWithData:data cache:NO];
  59. cell.image=image;
  60. [image release];
  61. return cell;
  62. }
  63. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  64. {
  65. return @”Hobby Information:”;
  66. }
  67. @end
复制代码


其中tableview,利用IB和相应的代码相连接。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值