应用程序在发布自定义表格单元格时崩溃

时间:2011-08-29 10:57:36

标签: iphone ios ipad uitableview

我的自定义TableViewCell发布时,我的应用崩溃了。

Cell会像cellForRowAtIndexPath中的以下内容一样进行初始化:

SearchTableViewCell *cell = (SearchTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[SearchTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

cell.nameLabel.text = @"some text";
cell.addressLabel.text = @"some more text";

细胞类本身看起来像这样

#import <UIKit/UIKit.h>

@class EGOImageView;

@interface SearchTableViewCell : UITableViewCell {
    UILabel *nameLabel;
    UILabel *addressLabel;
    EGOImageView *imageView;
}

@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *addressLabel;

- (UILabel *)labelWithColor:(UIColor*)color selectedColor:(UIColor*)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold frame:(CGRect)rect;
- (void)setThumb:(NSString*)thumb;

@end

的.m

#import "SearchTableViewCell.h"
#import "EGOImageView.h"

#import "UIView+Additions.h"

@implementation SearchTableViewCell

@synthesize nameLabel = _nameLabel, addressLabel = _addressLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

        // Initialization code
        UIView *myContentView = self.contentView;

        // Name
        _nameLabel = [self labelWithColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:16.0f  bold:YES frame:CGRectMake(140.0f, 16.0f, 181.0f, 21.0f)];
        [myContentView addSubview:_nameLabel];
        [_nameLabel release];


        // Adress
        _addressLabel = [self labelWithColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:13.0f  bold:YES frame:CGRectMake(140.0f, _nameLabel.bottom, 181.0f, 21.0f)];
        [myContentView addSubview:_addressLabel];
        [_addressLabel release];


        // Image
        imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
        imageView.frame = CGRectMake(9.0f, 9.0f, 120.0f, 80.0f);
        [myContentView addSubview:imageView];
        [imageView release];

    }
    return self;
}

- (UILabel *)labelWithColor:(UIColor*)color selectedColor:(UIColor*)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold frame:(CGRect)rect {

    UIFont *font;
    if(bold) {
        font = [UIFont boldSystemFontOfSize:fontSize];
    } else {
        font = [UIFont systemFontOfSize:fontSize];
    }

    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = color;
    label.highlightedTextColor = selectedColor;
    label.font = font;

    return label;
}


- (void)setThumb:(NSString*)thumb {
    imageView.imageURL = [NSURL URLWithString:thumb];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];

    if(!newSuperview) {
        [imageView cancelImageLoad];
    }
}

- (void)dealloc {
    [_addressLabel release];
    [_nameLabel release];
    [imageView release];
    [super dealloc];
}

@end

有没有人知道为什么我的应用程序在发布这样一个单元格时会崩溃?在dealloc方法上注释掉2个标签和图像视图,应用程序不会崩溃,但是会出现内存泄漏吗?

感谢所有提示!如果不清楚,请发表评论!

3 个答案:

答案 0 :(得分:1)

You already release the memory for that labels after adding to view,again you are trying to release  memory those objects are already relese  in dealloc method that's why it is killing.if you remove 3 statements in dealloc method  it will not crash.   

答案 1 :(得分:1)

imageView正在发布两次,一次创建时:

imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
imageView.frame = CGRectMake(9.0f, 9.0f, 120.0f, 80.0f);
[myContentView addSubview:imageView];
[imageView release];

和一次dealloc:

- (void)dealloc {
    [_addressLabel release];
    [_nameLabel release];
    [imageView release];
    [super dealloc];
}

答案 2 :(得分:0)

我认为问题在于您直接在initWithStyle函数中释放属性,并再次在dealloc中释放属性。尝试从initWithStyle中删除该版本:

此外,您在界面中使用_而不是_命名变量,但在实现

中使用_ @ synthesize'ing