自定义了一个Cell继承UItableViewCell,在nib文件中拖入一个cell,把identifier改成了和cellForRowAtIndexPath方法里的复用标识符。
问题来了,我运行程序后,拖动cell,程序崩溃。报错-[UIImage isKindOfClass:]: message sent to deallocated instance 0x1d504c70
上代码:ChatCustomCell1.h#import <UIKit/UIKit.h>@interface ChatCustomCell1 : UITableViewCell
{}@property (retain, nonatomic) IBOutlet UIImageView *leftFaceImageView;
@property (retain, nonatomic) IBOutlet UIImageView *rigthFaceImageView;
@property (retain, nonatomic) IBOutlet UIImageView *messageBackImage;
@property (retain, nonatomic) IBOutlet UILabel *leftNameLable;
@property (retain, nonatomic) IBOutlet UILabel *rightNameLable;
@property (retain, nonatomic) IBOutlet UILabel *leftTimeLable;
@property (retain, nonatomic) IBOutlet UILabel *rightTimeLable;
@property (retain, nonatomic) IBOutlet UILabel *messageLable;@end
ChatCustomCell1.m
#import "ChatCustomCell1.h"@implementation ChatCustomCell1
@synthesize leftFaceImageView=_leftFaceImageView;
@synthesize rigthFaceImageView=_rigthFaceImageView;
@synthesize messageBackImage=_messageBackImage;
@synthesize leftNameLable=_leftNameLable;
@synthesize rightNameLable=_rightNameLable;
@synthesize leftTimeLable=_leftTimeLable;
@synthesize rightTimeLable=_rightTimeLable;
@synthesize messageLable=_messageLable;- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
 
    }
    return self;
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];    // Configure the view for the selected state
}@end
uitableviewcell自定义Cell复用

解决方案 »

  1.   

    应该把这个cellForRowAtIndexPath函数内容贴出来吧
      

  2.   

    这种一般都是 内存释放 的问题,而且,UITableVIew才有处理cell的委托方法。
      

  3.   

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        static NSString *identifier = @"ApplicationCell";
        ApplicationCell *cell = (ApplicationCell*)[tableView dequeueReusableCellWithIdentifier: identifier];
        if(cell == nil)
        {
            printf("在这里cell == nil\n");
            [self.cellNib instantiateWithOwner:self options:nil];
            cell = tmpCell;
            self.tmpCell = nil;
        }
        NSInteger row = [indexPath row];
        ChatMessage *chatMessage = (ChatMessage *)[showMessageArray objectAtIndex:row];
        cell.message = chatMessage.message;    // 设置cell
        if (chatMessage.fromMyself)
        {
            UIImage *image = [UIImage imageNamed:@"online.png"];
            cell.leftFaceImage = image;
            [image release];
            cell.leftTime = chatMessage.messageTime;
            UIImage *image1 = [UIImage imageNamed:@"bubule2"];
            cell.messageBack = image1;
            [image1 release];
            AppDelegate *myApp = [[UIApplication sharedApplication] delegate];
            cell.leftName = myApp.loginName;
        }
        else
        {
            UIImage *image = [UIImage imageNamed:@"online.png"];
            cell.rigthFaceImage = image;
            [image release];
            cell.rightTime = chatMessage.messageTime;
            UIImage *image1 = [UIImage imageNamed:@"bubule1"];
            cell.messageBack = image1;
            [image1 release];
            cell.rightName = chatMessage.senderName;
        }
        return cell;
    }
    现在知道什么问题了,是复用过后,对队列里的cell内容进行修改导致的问题。但是麻烦有经验的给个建议,我想让这个cell在一种情况下左边显示图片和内容,另一种情况下右边显示。是改控件位置吗?我之前的做法是左右各加一个image,复用的时候就除了崩溃的问题。
      

  4.   

    在你的代码中,你使用的是ApplicationCell  而不是上述你定义的ChatCustomCell1。