これがあなたがやろうとしていることのコードです。必要に応じて、別の方法で表示できるように変更できます。これが正しいかどうか、またはメッセージを表示するために探していた他の方法があるかどうかを教えてください。
//
// 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
出力: