0

私はiPhoneアプリケーション開発の初心者です。私は現在XML解析に取り組んでいます。xmlファイルからレコードを抽出し、それを可変配列に格納しています。テーブルビューがあり、その可変配列からテーブルビューを読み込んでいます。デバッグで見つけたテーブルビューに18行が表示されます。問題は、10行まで下にスクロールするとうまく移動しますが、10行目が表示されるとアプリがクラッシュすることです。のエラーが発生しますEXC_BAD_ACCESS。これがセルがロードされる私のコードです。ありがとう。

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

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

        Location *record = [[Location alloc] init];

        record = [[parse locationarr] objectAtIndex:indexPath.row];

        cell.textLabel.text = record.locationname;

        return cell;
    }

}
4

3 に答える 3

2

投稿するコードにはいくつかの間違いがあります。最初にセルを返すだけで、テーブルはデキューのためにセルを返しません。

したがって、コードを変更すると、同様にその問題が解決されます。

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

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

   Location *record = [[Location alloc] init];
   record = [[parse locationarr] objectAtIndex:indexPath.row];

   cell.textLabel.text = record.locationname;

   return cell;
}

またalloc、毎回行う必要があることは非常にコストがかかるためinit、セルが必要になるたびに呼び出すクラスにwithを含めることをお勧めします。Beterはまだ、セルのデータのみを取得し、メソッドで長いメソッドを実行しないことです。LocationLocationtableVieew:cellForRowAtIndexPath:

于 2013-03-25T10:41:57.897 に答える
-1

このスニペットを書く必要があります

`record = [[parse locationarr] objectAtIndex:indexPath.row];

cell.textLabel.text = record.locationname;`

ifブロックから。

プログラミングをお楽しみください!

于 2013-03-25T10:48:07.763 に答える
-1

あなたの間違いは、if条件の内側に書かれている「セルを返す」であり、このように、行のタイトルラベルのテキストがサイド条件を設定します。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@ "cell"];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    Location *record = [[Location alloc] init];

}
else
{
  // get  record object here  by tag.  
}
record = [[parse locationarr] objectAtIndex:indexPath.row];

cell.textLabel.text = record.locationname;

 return cell;

}

于 2013-03-25T11:03:29.327 に答える