UItbaleview iOS中未显示单元格json内容
问题描述:
我创建了一个应用程序,它将从Web服务中获取信息。到目前为止,我通过使用NSLog显示内容来获取它,但是当我尝试将其加载到UITableViewCell中时,它不显示。这里是我的对于UItbaleview iOS中未显示单元格json内容
#import "TableViewController.h"
#import "JSON.h"
#import "SBJsonParser.h"
@interface TableViewController()
@end
@implementation TableViewController
@synthesize jsonurl,jsondata,jsonarray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
jsonurl=[NSURL URLWithString:@"http://minorasarees.com/category.php"];
jsondata=[[NSString alloc]initWithContentsOfURL:jsonurl];
self.jsonarray=[jsondata JSONValue];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonarray count];
}
- (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];
}
NSLog(@"1");
NSLog(@"%@",jsonarray);
cell.textLabel.text=[jsonarray objectAtIndex:indexPath.row];
NSLog(@"2");
return cell;
}
-(void)dealloc
{
[super dealloc];
[jsonarray release];
[jsondata release];
[jsonurl release];
}
@end
我已通过添加与UITableViewController..will文件,该文件是一个problem..Help请插入tableviewcontroller代码..
答
你的JSON包含字典的数组,所以你设置将表格视图单元格中的文本转换为无法工作的字典,因为字符串是预期的。这实际上应该会崩溃。
为了解决这个设置你的文字,该字典的类别属性:
cell.textLabel.text=[[jsonarray objectAtIndex:indexPath.row] valueForKey: @"category"];
除此之外还有其他的事情你的代码错误:[super dealloc]
必须在你dealloc
方法调用的最后一件事。你真的应该使用异步网络代码,阻止与网络主线程是不可接受的。
你记录了“[jsonarray objectAtIndex:indexPath.row]”吗? – 2012-07-22 07:30:07
ya.I可以让我的json内容在[jsonarray objectAtIndex:indexPath.row] .. – Dany 2012-07-22 07:34:11
你可以通过设置文本“cell.textLabel.text = @”test“;”? – 2012-07-22 07:36:25