gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:20:52 26 4
gpt4 key购买 nike

当我的自定义 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 个标签和 ImageView ,应用程序不会崩溃,但是会出现内存泄漏吗?

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

最佳答案

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];
}

关于iphone - 应用程序在发布自定义表格单元格时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com