1

アプリでjsonファイルを正常に解析しましたが、すべてをテーブルビューに表示しようとすると、表示されません.hereが私のコードです。

NSString *urlstr=[NSString stringWithFormat:@"http://minora.p41techdev.net/portal.php"];

NSURL *url=[NSURL URLWithString:urlstr];

NSData *data =[NSData dataWithContentsOfURL:url];

NSError *error;

NSArray *json=(NSMutableArray*) [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

//NSLog(@"%@",json);
NSDictionary *dict =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"a title", @"more data",nil] forKeys:[NSArray arrayWithObjects:@"titleKey",@"dataKey", nil]];


NSLog(@"1");

NSString *integ = [dict valueForKey:@"id"];
NSString *title=[dict valueForKey:@"title"];
NSString *category=[dict valueForKey:@"category"];
NSString *description=[dict valueForKey:@"description"];
NSString *spectitle=[dict valueForKey:@"spectitle"];
NSString *specvalue=[dict valueForKey:@"specvalue"];

NSArray *arr =[NSArray arrayWithObjects:integ,title,category,description,spectitle,specvalue, nil];

[tablearray addObject:arr];    
NSLog(@"%@",tablearray);





}

- (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)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [tablearray count];
NSLog(@"5");

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    cell.textLabel.text=[[tablearray objectAtIndex:indexPath.row] objectAtIndex:1];   


    return cell;

}

私のjsonファイルは次のようになります

[
{
    "id": "1",
    "category": "New Category1",
    "title": "New Product2",
    "description": "Please type a description1",
    "imgurl": "http://s.wordpress.org/about/images/wordpress-logo-notext-bg.png",
    "spectitle": "Specification",
    "specvalue": "Value"
},
{
    "id": "2",
    "category": "Mobile",
    "title": "Samsung",
    "description": "Please type a description",
    "imgurl": "http://s.wordpress.org/about/images/wordpress-logo-notext-bg.png",
    "spectitle": "Price",
    "specvalue": "100$"
}
]

ご指導お願いします...

私はこのようなスレッドの問題を抱えています

2012-07-20 19:36:03.504 tablevc[2253:f803] 1
2012-07-20 19:36:03.505 tablevc[2253:f803] 2
2012-07-20 19:36:03.507 tablevc[2253:f803] 4
2012-07-20 19:36:03.507 tablevc[2253:f803] 3
2012-07-20 19:36:03.508 tablevc[2253:f803] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-07-20 19:36:03.508 tablevc[2253:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x13d2022 0x1563cd6 0x137aa48 0x9b32cb 0xb6d28 0xb73ce 0xa2cbd 0xb16f1 0x5ad21 0x13d3e42    0x1d8a679 0x1d94579 0x1d194f7 0x1d1b3f6 0x1d1aad0 0x13a699e 0x133d640 0x13094c6 0x1308d84  0x1308c9b 0x12bb7d8 0x12bb88a 0x1c626 0x2ae2 0x2a55 0x1)
 terminate called throwing an exception
4

5 に答える 5

3

tablearray の初期化はどこにもありません。

これを viewDidLoad メソッドに追加します。

tablearray = [[NSMutableArray alloc] init];

また、配列内に配列を挿入していることもわかります。つまり、正しいデータ (この場合は NSString) にアクセスする必要がある場合は、正しいインデックスを使用する必要があります。

cell.textLabel.text=[[tablearray objectAtIndex:indexPath.row] objectAtIndex:1];   

タイトル文字列は内部配列のインデックス 1 に格納されるため、「objectAtIndex:1」を使用しました。NSDictionary を使用するのが、より優れた、より一般的なアプローチです。例えば:

NSDictionary *dict =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"a title", @"more data",nil] forKeys:[NSArray arrayWithObjects:@"titleKey",@"dataKey"]];

また、デリゲートがテーブルのセクションの正しい量を返すことを確認してください。

于 2012-07-20T13:32:40.297 に答える
2

テーブルビューのセクション数は少なくとも 1 つです...

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;//It should be at least one.......
    }

メソッドに次のコードを記述しcellForRowAtIndexPath:ます。そうしないと、エラーが発生します。

if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

お役に立てると思います。

于 2012-07-20T13:35:34.617 に答える
1

1つの要素を持つtablarrayがあります。この要素は、いくつかのエントリを含む配列です。データソースは1つのセルのみを宣言します(tablearray count、つまり1つ)。

cellForIndexPathメソッドは、とにかくjson配列の最初で最初の要素のみを常に検索します。

編集:無関係ですが、jsonのフィールドが設定されていない場合は、nilが返され、arrayWithObjectsの配列が終了します。これにより、範囲外のインデックスが作成される可能性があります。この方法には十分注意してください。足で自分を撃つのは非常に簡単です。

于 2012-07-21T05:59:43.457 に答える
1

テーブル配列に arr を追加した後に呼び出し[tableView reloadData]ます...

[tablearray addObject:arr];
[tableView reloadData]
NSLog(@"2");

それが役に立てば幸い

于 2012-07-20T13:33:43.377 に答える