0

TableView のカスタム TableViewCell を作成しましたが、dealloc メソッド (カスタム セル クラス) があるとアプリがクラッシュします。テーブル セル クラスに使用される以下のコードを参照してください。

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell {

    UILabel *nameLabel_;
    UILabel *dateLabel_;

}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;

@end


#import "CustomTableCell.h"

@implementation CustomTableCell

@synthesize nameLabel = nameLabel_;
@synthesize dateLabel = dateLabel_;


- (void) dealloc {

    [self.nameLabel release];
    self.nameLabel = nil;
    [self.dateLabel release];
    self.dateLabel = nil;

    [super dealloc];
}

@end

カスタム セル (cellForRowAtIndexPath) を作成するためのコード:

UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCell"];

if (Cell == nil){

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[CustomTableCell class]])
        {
            Cell = (CustomTableCell *)currentObject;
            break;
        }
    }
}

カスタム セルから dealloc メソッドを削除すると、すべて正常に動作します。そうしないと、例外が発生します(テーブルビューをスクロールすると):-[CALayer release]: 割り当て解除されたインスタンス 0x6fbc590 に送信されたメッセージ*

customTableViewCell に dealloc メソッドは必要ありませんか? この問題の解決策を見つけるのを手伝ってください。

4

1 に答える 1

1

一見すると、メソッドの書き方が-dealloc間違っています。このようにしてください:

- (void) dealloc {

    [nameLabel_ release];
    nameLabel_ = nil;
    [dateLabel_ release];
    dateLabel_ = nil;

    [super dealloc];
}

メソッドでアクセサーを使用しないでください-dealloc。ivar と直接連携します。

于 2012-12-06T01:18:19.683 に答える