Iphone JSON和动态表视图
有谁能给我提供一个JSON字符串到clickable tableview的好例子吗 我正在从服务器(已经工作)获取带有任务的json字符串,我需要用tableview发布它的视图。但它应该能够点击并给出该行的消息。Json结构:Iphone JSON和动态表视图,iphone,objective-c,json,Iphone,Objective C,Json,有谁能给我提供一个JSON字符串到clickable tableview的好例子吗 我正在从服务器(已经工作)获取带有任务的json字符串,我需要用tableview发布它的视图。但它应该能够点击并给出该行的消息。Json结构: { "messages":[{ "id": ...., "msg":...., "special":..., "comments":...}]} 有好的例子吗?我发现这个视频对学习非常有用 还请检查我的一些问题,我问了很多关于
{ "messages":[{
"id": ....,
"msg":....,
"special":...,
"comments":...}]}
有好的例子吗?我发现这个视频对学习非常有用 还请检查我的一些问题,我问了很多关于JSON的问题,但视频是一个很好的前进方式 更多链接:)
您必须使用类库解析JSON响应,然后根据数据创建数组或字典,并使用此字典和数组填充tableview 如果您可以提供json响应,那么我可以帮助您解析它 我添加了一些显示解析的代码片段:
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
//to display driver name in drop down
NSArray * messagesArray = [[NSArray alloc]init];
messagesArray= [jsonData objectForKey:@"messages"];
for(int i=0;i<[driverNameArray count];i++)
{
NSDictionary *tempDictionary = [messagesArray objectAtIndex:i];
if([tempDictionary objectForKey:@"id"]!=nil)
{
[idAry addObject:[tempDictionary objectForKey:@"id"]];
}
if([tempDictionary objectForKey:@"msg"]!=nil)
{
[msgAry addObject:[tempDictionary objectForKey:@"msg"]];
}
if([tempDictionary objectForKey:@"special"]!=nil)
{
[specialAry addObject:[tempDictionary objectForKey:@"special"]];
}
if([tempDictionary objectForKey:@"comments"]!=nil)
{
[commentsAry addObject:[tempDictionary objectForKey:@"comments"]];
}
}
SBJsonParser*jsonParser=[SBJsonParser new];
NSDictionary*jsonData=(NSDictionary*)[jsonParser objectWithString:responseData错误:nil];
//在下拉列表中显示驱动程序名称的步骤
NSArray*messagesArray=[[NSArray alloc]init];
messagesArray=[jsondataobjectforkey:@“messages”];
对于(int i=0;i,以下是您尝试执行的操作的代码。如果需要,您可以修改以在中以不同的方式查看,如果这是正确的,请告诉我,或者您希望以其他方式显示消息
//
// MyViewController.h
// Test
//
// Created by Syed Arsalan Pervez on 2/22/13.
// Copyright (c) 2013 SAPLogix. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyViewController : UITableViewController
{
NSArray *_messages;
}
@end
//
// MyViewController.m
// Test
//
// Created by Syed Arsalan Pervez on 2/22/13.
// Copyright (c) 2013 SAPLogix. All rights reserved.
//
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *JSONString = @"{\"messages\":[{\"id\":\"1\",\"msg\":\"Message Line\",\"special\":\"Special Line\",\"comments\":\"Comments Line\"},{\"id\":\"2\",\"msg\":\"Message Line\",\"special\":\"Special Line\",\"comments\":\"Comments Line\"}]}";
NSError *error = nil;
NSDictionary *JSONObj = [NSJSONSerialization JSONObjectWithData:[JSONString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
if (!error)
{
_messages = [[JSONObj valueForKey:@"messages"] retain];
}
else
{
NSLog(@"Error: %@", error);
}
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [_messages count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 3;
}
- (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];
}
NSDictionary *_message = [_messages objectAtIndex:indexPath.section];
switch (indexPath.row)
{
case 0:
cell.textLabel.text = [_message valueForKey:@"msg"];
break;
case 1:
cell.textLabel.text = [_message valueForKey:@"special"];
break;
case 2:
cell.textLabel.text = [_message valueForKey:@"comments"];
break;
}
_message = nil;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDictionary *_message = [_messages objectAtIndex:section];
return [NSString stringWithFormat:@"Message %@", [_message valueForKey:@"id"]];
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *_message = [_messages objectAtIndex:indexPath.section];
NSMutableString *string = [NSMutableString new];
[string appendFormat:@"Message %@: ", [_message valueForKey:@"id"]];
switch (indexPath.row)
{
case 0:
[string appendString:[_message valueForKey:@"msg"]];
break;
case 1:
[string appendString:[_message valueForKey:@"special"]];
break;
case 2:
[string appendString:[_message valueForKey:@"comments"]];
break;
}
_message = nil;
[[[[UIAlertView alloc] initWithTitle:@"Alert" message:string delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
[string release];
}
- (void)dealloc
{
[_messages release];
[super dealloc];
}
@end
//
//MyViewController.h
//试验
//
//由Syed Arsalan Pervez于2013年2月22日创建。
//版权所有(c)2013 SAPLogix。保留所有权利。
//
#进口
@接口MyViewController:UITableViewController
{
NSArray*_消息;
}
@结束
//
//MyViewController.m
//试验
//
//由Syed Arsalan Pervez于2013年2月22日创建。
//版权所有(c)2013 SAPLogix。保留所有权利。
//
#导入“MyViewController.h”
@接口MyViewController()
@结束
@MyViewController的实现
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
NSString*JSONString=@“{”messages\”:[{“id\”:“1\”,“msg\”:“messageline\”,“special\”:“special Line\”,“comments\”:“comments Line\”,{“id\”:“2\”,“msg\”:“messageline\”,“special\”:“special\”:“special Line\”,“comments\”:“comments Line\”,“comments\”:“comments Line\”);
n错误*错误=nil;
NSDictionary*JSONObj=[NSJSONSerialization JSONObjectWithData:[JSONString dataUsingEncoding:NSUTF8StringEncoding]选项:NSJSONReadingAllowFragments错误:&error];
如果(!错误)
{
_messages=[[JSONObj valueForKey:@“messages”]retain];
}
其他的
{
NSLog(@“错误:%@”,错误);
}
[self.tableView重载数据];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回[_消息计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
返回3;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
NSDictionary*\u message=[\u messages objectAtIndex:indexath.section];
开关(indexPath.row)
{
案例0:
cell.textlab.text=[\u消息值forkey:@“msg”];
打破
案例1:
cell.textlab.text=[\u消息值forkey:@“特殊”];
打破
案例2:
cell.textlab.text=[\u消息值forkey:@“注释”];
打破
}
_消息=零;
返回单元;
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分
{
NSDictionary*\u message=[\u messages objectAtIndex:section];
返回[NSString stringWithFormat:@“Message%@”,“[\u Message valueForKey:@“id]”;
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[tableView取消行索引路径:indexPath动画:是];
NSDictionary*\u message=[\u messages objectAtIndex:indexath.section];
NSMutableString*字符串=[NSMutableString new];
[string appendFormat:@“Message%@:”,[u Message valueForKey:@“id”];
开关(indexPath.row)
{
案例0:
[string appendString:[_messagevalueforkey:@“msg”];
打破
案例1:
[string appendString:[_messagevalueforkey:@“special”];
打破
案例2:
[string appendString:[_messagevalueforkey:@“comments”];
打破
}
_消息=零;
[[[UIAlertView alloc]initWithTitle:@“警报”消息:字符串委托:nil CancelButtontTitle:@“确定”其他按钮:nil]自动释放]显示];
[字符串释放];
}
-(无效)解除锁定
{
[_信息发布];
[super dealoc];
}
@结束
输出:
我不明白,你想用json值填充tableview吗?我想将json值解析为不同的表行。通过按table line来提醒该行的消息,我有一个愚蠢的问题。这在iphone 4.2版本上有效吗?:)是的,这取决于,如果你需要帮助,只需发布你的代码,其他人会帮助和解释。如果这有助于您,请标记更正。我已向您提供JSON的返回。“…”表示它可以返回的内容(取决于datanase中的内容)。第一个类似于id:1,msg:test,special:0,comments:0感谢您的帮助:)即使使用ARC,您的代码也有很多漏洞。请尝试避免无用的alloc。@SAPLogix不是无用的alloc。@Dilip NSA