JSON和动态TableView
任何人都可以提供任何JSON字符串的可点击tableview的好例子吗?JSON和动态TableView
我得到的json字符串与服务器(已经工作)的任务,我需要发布它的视图与tableview。但它应该能够点击并给出该行的消息。 Json结构:
{ "messages":[{
"id": ....,
"msg":....,
"special":...,
"comments":...}]}
任何好的例子?
我发现这个视频非常有用的学习。
http://www.youtube.com/watch?v=9MEnvlqP-wU
还检查了我的一些问题,我问了很多有关JSON,但视频是向前迈进的好方法。
更多链接:)
我有一个愚蠢的问题。这将工作在iPhone 4.2的版本? :) – 2013-02-22 13:49:23
是啊,这取决于,如果你需要帮助只是发布你的代码和其他人会帮助和解释。如果这有帮助,请您标记正确的 – Brent 2013-02-22 14:27:48
你必须解析使用JSON响应库,像SBJson,然后创建从数据数组或字典,并使用这本字典和数组填充你的tableview。
如果你能提供你的json响应,我可以帮你解析它。
我是addind一些代码片段,显示解析:
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"]];
}
}
的,我已经使用commentsAry,specialAry,msgAry,idAry保存数据,并可以用于填充tableview中所有的数组。
我已经为您提供了JSON的回报。 “...”表示它可以返回的内容(取决于datanase中的内容)。首先是像ID:1,味精:测试,特殊:0,评论:0 – 2013-02-22 14:08:18
感谢您的帮助:) – 2013-02-22 14:28:40
即使您使用ARC,您的代码有很多泄漏尝试避免无用的分配。 – SAPLogix 2013-02-22 14:29:56
以下是您正在尝试做的事情的代码。如果您愿意,您可以修改为可以以不同方式查看,如果这是正确的,或者您有其他方式希望显示这些消息,请通知我。
//
// 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
输出:
我不understad,要填充使用JSON价值的实现代码如下? – Dilip 2013-02-22 13:47:10
我想解析JSON值到不同的表格行。并通过按表行来提醒该行的消息 – 2013-02-22 13:50:53