TTTableViewController和JSon
问题描述:
我在加载tableview中的数据时出现问题。当视图加载时,我看到表格,但没有填充单元格。TTTableViewController和JSon
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"]);
如果被检索显示所有的主题行,但不填充表。
答
而不是
self.dataSource = [TTListDataSource
dataSourceWithItems:tableItems.items];
为什么你不这样做呢?
self.dataSource = tableItems;
您可能还需要寻找到创建模型去与你的数据源 - 也就是说,扩展TTModel类(或TTURLRequestModel,因为你是在做一个URL请求)。