从json传递数据显示表格实例
2015-12-15 13:01
531 查看
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UITableView* table; NSMutableArray* parseResult; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //获取本地json文件 NSString* jsonPath=[[NSBundle mainBundle]pathForResource:@"books" ofType:@"json"]; //把json文件转成NSData类型 NSData* data=[NSData dataWithContentsOfFile:jsonPath]; //解析json数据 返回OC对象 数据转对象用JSONObjectWithData方法 parseResult=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; table=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, 568-20) style:UITableViewStylePlain]; table.dataSource=self; table.delegate=self; [self.view addSubview:table]; //把NSData文件解析json成数据 NSData* data1=[NSJSONSerialization dataWithJSONObject:parseResult options:0 error:nil]; //把NSData文件转成NSString格式 用于直接输出 NSString* json=[[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding]; NSLog(@"%@",json); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return parseResult.count; } #pragma mark 表示每一行显示什么数据 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { //内存优化 static NSString * identity=@"cell"; //tableview 根据标识复制出一个cell UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identity]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identity]; } NSDictionary* dic=parseResult[indexPath.row]; cell.textLabel.text=[dic valueForKey:@"title"]; cell.detailTextLabel.text=[dic valueForKey:@"author"]; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; }
相关文章推荐
- JS报错 Uncaught SyntaxError: missing ) after argument list
- JS正则表达式验证数字非常全
- JavaScript为事件句柄绑定监听函数实例详解
- Gson解析嵌套JSON格式数据
- JavaScript常用获取宽高的方法
- js中var
- Json详解
- JavaScript获取当前cpu使用率的方法
- js-----时间的联动框
- c# 中执行js的问题
- json接口相关(建议结合JFinal框架)
- js简单网速测试方法完整实例
- js数组和对象的取赋值
- JSP如何实现分页功能
- Sublime text3 JS语法检测工具安装及使用
- JavaScript基础——事件
- Javascript中定义类
- java&nbsp;&amp;&nbsp;javascript&nbsp;生成随机数
- JSON数据转换
- [js][json]前台页面中json和字符串…