首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

iPhone下的JSON JSON+UITableView

2012-08-26 
iPhone上的JSONJSON+UITableView好,开始打代码吧。1,首先copy JSON库到当前的Project里面。2,建立一个数据源

iPhone上的JSON JSON+UITableView


好,开始打代码吧。

1,首先copy JSON库到当前的Project里面。

iPhone下的JSON    JSON+UITableView

2,建立一个数据源类。我给它起名叫MyDataSource, 看看里面都有什么吧:

#import "MyTableViewController.h"#import "MyDataSource.h"?@implementation MyTableViewController?- (id)initWithStyle:(UITableViewStyle)style{if (self = [super initWithStyle:style]) {myData = [[MyDataSource dataSource] retain];//在这里我们初始化myData,其实就是一个id对象//传入由MyDataSource解析出的NSDictionary}return self;}?#pragma mark Table view methods?- (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]; //“我们是runtime的好市民”...release就好Alan......    [super dealloc];}@end

4,在主程序代理 xxxAppDelegate 里面初始化这个UITableViewController然后添加它的view到window的subview中就OK拉!

5,编译运行,没有错误就万事大吉!大吉!

阿弥陀佛,祝各位愉快~

热点排行
Bad Request.