0

UITableViewWebサービスからのデータを読み込もうとしています。すべてが正常に機能しています。UITableViewしかし、デリゲートメソッドを使用してレコードが表示されているときはいつでも、以下の-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathような例外が発生します-

ここに画像の説明を入力してください

これが私のコードです-

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell; //= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Reuse_Cell"];
    UILabel *label = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectedBackgroundView = [[UIView alloc]init];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

        label = [[UILabel alloc]initWithFrame:CGRectZero];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setNumberOfLines:1];
        [label setFont:[UIFont fontWithName:@"Trebuchet MS" size:18]];
        [label setTag:1];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        [[cell contentView] addSubview:label];

    }

    if (!label)
        label = (UILabel *)[cell viewWithTag:1];

    [label setText:[[webserviceRecords objectAtIndex:indexPath.row] objectForKey:@"catName"]];
    [label setFrame:CGRectMake(10.0, 5.0, 225.0, 35.0)];

    cell.selectedBackgroundView.backgroundColor = RGBACOLOR(151.0, 191.0, 69.0, 1.0);
    cell.layer.borderWidth = 2.0f;
    cell.layer.borderColor = RGBCOLOR(214, 218, 1).CGColor;

    return cell;
}

マイアレイレコード-

http://pastie.org/7120406

カスタムinitメソッド

- (id)initwithInteger:(NSInteger )integer
{
    self = [super init];
    if (self) {
        startingpage = integer;
        webserviceRecords = [[NSMutableArray alloc]init];
    }
    return self;
}

NumberofRowsInSection

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Row count - %d", [webserviceRecords count]);
    return [webserviceRecords count];
}
4

2 に答える 2

1

selectedBackgroundViewセルに値があることを確認する前に、セルを設定しています。それを行う前に、最初にセルがnilであるかどうかを確認する必要があります。

ちなみに、無料のSensibleTableViewフレームワークを使用して配列を表示する方がはるかに簡単な場合があります。フレームワークは、配列からすべてのセル(すべての詳細ビューを含む)を自動的に生成し、必要に応じてWebサービスから直接データをフェッチすることもできます。あなたに多くの頭痛と手作業を節約するはずです。お役に立てれば。

于 2013-03-26T05:38:01.090 に答える
1

本当にばかげた間違いがありました。私は最初に以下のように解析したJSONデータに直接解析しましたNSMutableArray-

webserviceRecords = [NSJSONSerialization JSONObjectWithData:mutableData options:0 error:&error];

これの代わりに、私はこれを以下のように修正しました-

NSArray *array = nil;
array = [NSJSONSerialization JSONObjectWithData:mutableData options:0 error:&error];
webserviceRecords = [array mutableCopy];

問題を解決するのに役立ちます。私の質問に答えてくれてありがとう。

乾杯!

于 2013-03-26T05:53:21.923 に答える