Iphone利用JSON传递数据,展示在Table界面中

上面用到了json传递的数据,有关json部分,iphone sdk虽然没有支持,但是第三方已经写好了。

json 参考:http://code.google.com/p/json-framework/

 

下面是具体的代码实现:

数据加载:

    #import “MyDataSource.h”

    #import “JSON.h”

    @implementation MyDataSource

    + (NSDictionary *)fetchLibraryInformation

    {

    NSString *urlString = [NSString stringWithFormat:@"http://wangjun.easymorse.com/wp-content/video/hello.jison"];

    NSURL *url = [NSURL URLWithString:urlString];

    NSLog(@”fetching library data”);

    return [self fetchJSONValueForURL:url];

    }

    + (id)fetchJSONValueForURL:(NSURL *)url

    {

    NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url

    encoding:NSUTF8StringEncoding error:nil];

     

    id jsonValue = [jsonString JSONValue];

    [jsonString release];

    return jsonValue;

    }

    @end

table数据展示:

#import “JSONTableTestViewController.h”

#import “MyDataSource.h”

@implementation JSONTableTestViewController

@synthesize myData;

- (void)viewDidLoad {

NSLog(@”加载数据“);

myData = [[MyDataSource fetchLibraryInformation] retain];

}

- (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 {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [myData count]; //有多少个section,也就是“几家”

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[myData valueForKey:[[myData allKeys] objectAtIndex:section]] count];

//这里我们需要告诉UITableViewController每个section里面有几个,也就是“一家里面有几口人”

}

- (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.text = [[myData valueForKey:[[myData allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

//这句看上去复杂,但是其实不过是在特定section里面找到对应的array,

//然后在array中找到indexPath.row所在的内容

return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [[myData allKeys] objectAtIndex:section];

//这里设置对应section的名字,很简单allKey返回所有的键值为一个array,也就是“张家”,“李家”

//然后用objectAtIndex: 来找出究竟是哪一个就可以了!

}

- (void)dealloc {

[myData release];

[super dealloc];

}

@end

http://wangjun.easymorse.com/?p=825


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值