0

UITableViewControllerは、excbadaccesssエラーでクラッシュします。これは私のコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *cellText ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }
    if (indexPath.row == 0) 
    {
        cellText = @"Mail1";
    }
    else if (indexPath.row == 1) 
    {
        cellText = @"Mail2";
    }
    else if (indexPath.row == 2)
    {
        cellText = @"Mail3";
    }
    else if(indexPath.row == 3)
    {
        cellText = @"Mail4";
    }
    else
    {
        cellText = @"Mail5";
    }
    cell.textLabel.text = cellText;
    return cell;
}

これはxcodeが提供したログメッセージです

XXXXProject [1366:f803] * -[XXXXViewController tableView:cellForRowAtIndexPath:]:割り当て解除されたインスタンス0x6a793f0に送信されたメッセージ

4

2 に答える 2

0

セルを初期化する必要はないと思います。おそらくストーリーボードがセルを初期化します。

ss

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

    // Configure the cell...
    NSString *cellText;
    if (indexPath.row == 0) 
    {
        cellText = @"Mail1";
    }
    else if (indexPath.row == 1) 
    {
        cellText = @"Mail2";
    }
    else if (indexPath.row == 2)
    {
        cellText = @"Mail3";
    }
    else if(indexPath.row == 3)
    {
        cellText = @"Mail4";
    }
    else
    {
        cellText = @"Mail5";
    }
    cell.textLabel.text = cellText;
    return cell;
}

このサンプル プロジェクトを GitHub からダウンロードして、実行するだけです。

https://github.com/weed/p120817_TableTest

于 2012-08-17T09:49:39.940 に答える
0

まったく同じ質問が数時間前に StackOverflow に投稿されました。(デリゲートと呼ばれるテーブル ビューではなく、そのアクションの UIButton があります)。問題はおそらく、テーブル ビュー コントローラーへの強い参照がないため、作成直後に割り当てが解除されていることです。前の質問に対する私の回答を参照してください。

于 2012-08-17T11:54:55.583 に答える