UITableCell AccessoryView:设置accessoryView等于UIImageView无限循环

编辑:我已经找到了我自己的答案,但这里是为了其他需要它的人:UIImageViews无法共享,因此每个可见单元格需要不同的每个UIImageView实例化。 现在你知道了。

我有一个自定义表,有两种类型的单元格。 一个单元格设置为在类型复选标记的普通附件之间切换。 另一个单元格设置为具有自定义图像作为附件类型。 当选择附件图像变为其相反类型时,显示“邀请”或“邀请”消息。

我已经将代码中的代码缩小到以下内容,在我的tableView:cellForRowAtIndexPath委托方法中找到。

if(indexPath.section == 0){ cell = [tableView dequeueReusableCellWithIdentifier:self.directCellID]; cellValue = [self.contactsUsingApp objectAtIndex:indexPath.row]; cell.imageView.image = [self getContactImage:indexPath.row]; //vvvvvvvvvvvvvvvvv This is the section at fault vvvvvvvvvvvvvvvvv if([self.selectedContactsUsingApp containsObject:indexPath]) cell.accessoryView = self.invitedStatus; else cell.accessoryView = self.notInvitedStatus; //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } 

如果我注释掉那部分我不再有失控的内存使用(模拟器向我显示有某种常量分配正在进行,它从40Mb开始后通过了1.29Gb)但显然,图像不再显示。

如果重要,UIImageViews初始化如下:

 UIImage *invite = [self imageWithImage:[UIImage imageNamed: @"invite_btn.png"] scaledToSize:CGSizeMake(40, 20)]; UIImage *invited = [self imageWithImage:[UIImage imageNamed: @"invited_btn.png"] scaledToSize:CGSizeMake(40, 20)]; self.notInvitedStatus = [[UIImageView alloc]initWithImage:invite]; self.invitedStatus = [[UIImageView alloc]initWithImage:invited]; 

(imageWithImage:scale是一个函数,它将resize的图像返回到适当的比例,以解释此处发现的视网膜: 调整UIImage大小的最简单方法? )

当我选择其中一个单元格时会发生相同的冻结,因为我的tableView:didSelectRowAtIndexPath方法使用与初始化方法相同的切换逻辑。

帮帮我?

我已经找到了我自己的答案,但这里是需要它的其他人:UIImageViews无法共享,因此每个可见单元格需要不同的每个UIImageView实例化。 现在你知道了。