0

Webサービスから情報を取得するアプリを作成しました。これまでのところ、NSLogを使用してコンテンツを表示することで取得しましたが、UITableViewCellにロードしようとすると表示されません。

#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でファイルを追加してtableviewcontrollerを挿入しました..それは問題になるでしょうか..助けてください..

4

1 に答える 1

2

JSONには辞書の配列が含まれているため、テーブルビューセルのテキストを辞書に設定していますが、文字列が予想されるため機能しません。これは実際にクラッシュするはずです。

それを解決するには、テキストをその辞書のカテゴリ プロパティに設定します。

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

これに加えて、コードには他にも問題があります。メソッド[super dealloc]で呼び出す最後のものである必要がありますdealloc。また、非同期ネットワーク コードを使用する必要があります。ネットワークでメイン スレッドをブロックすることは受け入れられません。

于 2012-07-22T07:49:09.230 に答える