TTTableViewController和JSon

时间:2011-03-12 18:16:40

标签: objective-c json three20

我在tableview中加载数据时遇到问题。当视图加载时,我会看到表格,但单元格中没有任何内容。

PMs.h

@interface Pms : TTTableViewController {
NSArray *rows;
TTTableSubtitleItem *item;
}
@property (retain,nonatomic) NSArray *rows;

PMs.m

- (void)dealloc {
[rows release];
[super dealloc];

}
- (id)init {
//[self.navigationController setNavigationBarHidden:NO];
if (self = [super init]) {
    self.variableHeightRows = YES;
    self.dataSource = nil;

}
return self;
}



-(void)getLatest{

NSURL *url = [NSURL URLWithString:@"http://URL.com/pms.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;

// In "real" code you should surround this with try and catch
NSDictionary * dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
NSMutableArray *zodiaq = (NSMutableArray *)[[dict objectForKey:@"users"] retain];
TTListDataSource* tableItems = [[[TTListDataSource alloc] init] autorelease];

for(NSDictionary *dict in zodiaq){
    item = [TTTableSubtitleItem itemWithText:[dict objectForKey:@"from_user"] subtitle:[dict objectForKey:@"subject"]
                                    imageURL:nil 
                                defaultImage:nil
                                         URL:@"tt://tableItemTest" 
                                accessoryURL:nil];

    [tableItems.items addObject:item];      
}
self.dataSource = [TTListDataSource
                   dataSourceWithItems:tableItems.items]; 

[jsonreturn release];
}

- (void)loadView {
[super loadView];
[self setTableViewStyle:UITableViewStylePlain];
self.tableView.frame = CGRectMake(0, 44, 320, 396);
[self getLatest];

}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}



@end

当我做的时候

NSLog(@"Data: %@",[dict objectForKey:@"subject"]);

如果显示正在检索的所有主题行,但不填充表格。

1 个答案:

答案 0 :(得分:0)

而不是

self.dataSource = [TTListDataSource
                   dataSourceWithItems:tableItems.items];

你为什么不这样做?

self.dataSource = tableItems;

您可能还想研究创建一个与您的dataSource一起使用的模型 - 即一个扩展TTModel(或TTURLRequestModel,因为您正在发出URL请求)的类。