【问题标题】:TTTableViewController and JSonTTTableViewController 和 JSon
【发布时间】:2011-03-12 18:16:39
【问题描述】:

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

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"]);

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

【问题讨论】:

    标签: objective-c json three20


    【解决方案1】:

    代替

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

    你为什么不这样做呢?

    self.dataSource = tableItems;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多