我将CustomImageView添加到UITableViewCell。
UITableViewCell *cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell1"] autorelease];
}
CustomImageView *customIV = [[CustomImageView alloc] init];
[cell.contentView addSubView:customIV];
[customIV release];
但是当我尝试重新加载tableview时,会发生错误。
错误调用堆栈如下所示。
输出字符串如下。
- [CustomImageView superview]:发送到解除分配的实例0x1f848f30的消息
答案 0 :(得分:1)
CustomImageView *customIV = [[CustomImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[cell.contentView addSubView:customIV];
当我释放记忆时,我就完成了 所以根据我的说法,不需要释放,因为它会释放内存。
希望它会对你有所帮助。
谢谢。
答案 1 :(得分:1)
尝试评论此行[customIV release];
&运行时,重新加载数据时不应该崩溃。
这背后的原因是它每次尝试创建新的自定义视图&释放它,因此导致系统上的额外负载和发生了崩溃。
答案 2 :(得分:1)
您只想将图像添加到每个单元格一次。将您的代码更改为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell1"] autorelease];
CustomImageView *customIV = [[CustomImageView alloc] init];
[cell.contentView addSubView:customIV];
[customIV release];
}
return cell;
}
如果这不起作用,那么您需要显示完整的cellForRowAtIndexPath
方法。只显示部分代码,很难提供帮助。
答案 3 :(得分:0)
发生此错误的原因是每次创建CustomImageView
时创建的cell
对象。
所以,最好的方法是首先初始化CustomImageView
的对象,然后创建UITableView
之类的,
CustomImageView *customIV
将其放入.h file
,然后@synthesize
.m File
(将此代码置于UITableView
)
self.customIV = [[CustomImageView alloc] init];
然后创建UITableView
self.tablView = [[UITableView alloc]init];
.
.
.
.
在cellForRowAtIndexPath
中只添加[cell.contentView addSubView:self.customIV];