2012-07-22 45 views
0

我創建了一個應用程序,它將從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代碼..

+0

你記錄了「[jsonarray objectAtIndex:indexPath.row]」嗎? – 2012-07-22 07:30:07

+0

ya.I可以讓我的json內容在[jsonarray objectAtIndex:indexPath.row] .. – Dany 2012-07-22 07:34:11

+0

你可以通過設置文本「cell.textLabel.text = @」test「;」? – 2012-07-22 07:36:25

回答

2

你的JSON包含字典的數組,所以你設置將表格視圖單元格中的文本轉換爲無法工作的字典,因爲字符串是預期的。這實際上應該會崩潰。

爲了解決這個設置你的文字,該字典的類別屬性:

cell.textLabel.text=[[jsonarray objectAtIndex:indexPath.row] valueForKey: @"category"]; 

除此之外還有其他的事情你的代碼錯誤:[super dealloc]必須在你dealloc方法調用的最後一件事。你真的應該使用異步網絡代碼,阻止與網絡主線程是不可接受的。

+0

感謝您的大力幫助......此作品... – Dany 2012-07-22 07:52:08

+0

如果萬一我的字典值超過類別,標題,編號等...我需要全部顯示在一個單元格...我怎麼能得到它... – Dany 2012-07-22 08:04:47

+0

構建一個包含它們的單個字符串(使用'[NSString stringWithFormat:]')或創建包含多個標籤的自定義表格視圖單元格。 – Sven 2012-07-22 08:07:10