我有一个uitableview,我已经为每一行分配了图像。 tableView显示聊天消息和聊天气泡的图像(如ios消息传递应用程序)。
滚动非常慢,图像似乎在上下滚动时重复。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"tableView method called AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
//static NSString *CellIdentifier = @"Cell";
//---add this---
UILabel* dateLabel = nil;
UILabel* messageLabel = nil;
UIImageView *imageView_top_left = nil;
UIImageView *imageView_top_middle = nil;
UIImageView *imageView_top_right = nil;
UIImageView *imageView_middle_left = nil;
UIImageView *imageView_middle_right = nil;
UIImageView *imageView_middle_middle = nil;
UIImageView *imageView_bottom_left = nil;
UIImageView *imageView_bottom_middle = nil;
UIImageView *imageView_bottom_right = nil;
/*UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}*/
ChatMessage *chatMessage = [self.chatMessagesList objectAtIndex:indexPath.row];
// Check whether this message is to me
if (![chatMessage.fromId isEqualToString:myId]){
/////////// changed //////////
static NSString *CellIdentifier = @"Cell1";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
////////////////////////////
NSLog(@"AAAAAAAAAAAAAAAAA");
/////////////////// image ////////////////////////////////////////////////////////////
NSURL *avatarUrl = [NSURL URLWithString:
@"http://www.mobyn.com/mobyn_v3/application/upload/profile_images/default.jpg"];
UIImage *avatarImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:avatarUrl]];
UIImageView *avatar = [[UIImageView alloc] initWithImage:avatarImage];
avatar.frame = CGRectMake(0, 23, 60, 60);
[cell.contentView addSubview:avatar];
//[avatarImage release];
//[avatar release];
//////////////////////////////////////////////////////////////////////////////////////
// If YES, then create the row with the corresponding bubble
//---add this---
//---date---
// dateLabel = [[[UILabel alloc] init] autorelease];
//dateLabel.tag = DATELABEL_TAG;
//[cell.contentView addSubview: dateLabel];
//---top left---
imageView_top_left = [[[UIImageView alloc] init] autorelease];
imageView_top_left.tag = IMAGEVIEW_TAG_1;
[cell.contentView addSubview: imageView_top_left];
//---top middle---
imageView_top_middle = [[[UIImageView alloc] init] autorelease];
imageView_top_middle.tag = IMAGEVIEW_TAG_2;
[cell.contentView addSubview: imageView_top_middle];
//---top right---
imageView_top_right = [[[UIImageView alloc] init] autorelease];
imageView_top_right.tag = IMAGEVIEW_TAG_3;
[cell.contentView addSubview: imageView_top_right];
//---middle left---
imageView_middle_left = [[[UIImageView alloc] init] autorelease];
imageView_middle_left.tag = IMAGEVIEW_TAG_4;
[cell.contentView addSubview: imageView_middle_left];
//---middle middle---
imageView_middle_middle = [[[UIImageView alloc] init] autorelease];
imageView_middle_middle.tag = IMAGEVIEW_TAG_5;
[cell.contentView addSubview: imageView_middle_middle];
//---middle right---
imageView_middle_right = [[[UIImageView alloc] init] autorelease];
imageView_middle_right.tag = IMAGEVIEW_TAG_6;
[cell.contentView addSubview: imageView_middle_right];
//---bottom left---
imageView_bottom_left = [[[UIImageView alloc] init] autorelease];
imageView_bottom_left.tag = IMAGEVIEW_TAG_7;
[cell.contentView addSubview: imageView_bottom_left];
//---bottom middle---
imageView_bottom_middle = [[[UIImageView alloc] init] autorelease];
imageView_bottom_middle.tag = IMAGEVIEW_TAG_8;
[cell.contentView addSubview: imageView_bottom_middle];
//---bottom right---
imageView_bottom_right = [[[UIImageView alloc] init] autorelease];
imageView_bottom_right.tag = IMAGEVIEW_TAG_9;
[cell.contentView addSubview: imageView_bottom_right];
//---message---
messageLabel = [[[UILabel alloc] init] autorelease];
messageLabel.tag = MESSAGELABEL_TAG;
[cell.contentView addSubview: messageLabel];
//---set the images to display for each UIImageView---
imageView_top_left.image =
[UIImage imageNamed:@"bubble_top_left2.png"];
imageView_top_middle.image =
[UIImage imageNamed:@"bubble_top_middle2.png"];
imageView_top_right.image =
[UIImage imageNamed:@"bubble_top_right2.png"];
imageView_middle_left.image =
[UIImage imageNamed:@"bubble_middle_left2.png"];
imageView_middle_middle.image =
[UIImage imageNamed:@"bubble_middle_middle2.png"];
imageView_middle_right.image =
[UIImage imageNamed:@"bubble_middle_right2.png"];
imageView_bottom_left.image =
[UIImage imageNamed:@"bubble_bottom_left2.png"];
imageView_bottom_middle.image =
[UIImage imageNamed:@"bubble_bottom_middle2.png"];
imageView_bottom_right.image =
[UIImage imageNamed:@"bubble_bottom_right2.png"];
///////////////////////////////////////////////
//---calculate the height for the label---
int labelHeight = [self labelHeight:messsageForTheRow];
labelHeight -= bubbleFragment_height;
if (labelHeight<0) labelHeight = 0;
//---you can customize the look and feel for the date for each message here---
dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0);
dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor];
//---top left---
imageView_top_left.frame =
CGRectMake(bubble_x, bubble_y, bubbleFragment_width, bubbleFragment_height);
//---top middle---
imageView_top_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width, bubble_y,
bubbleFragment_width, bubbleFragment_height);
//---top right---
imageView_top_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2), bubble_y,
bubbleFragment_width, bubbleFragment_height);
//---middle left---
imageView_middle_left.frame =
CGRectMake(bubble_x, bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle middle---
imageView_middle_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width, bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle right---
imageView_middle_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2),
bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---bottom left---
imageView_bottom_left.frame =
CGRectMake(bubble_x, bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---bottom middle---
imageView_bottom_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width,
bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height);
//---bottom right---
imageView_bottom_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2),
bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---you can customize the look and feel for each message here---
messageLabel.frame =
CGRectMake(bubble_x + 10, bubble_y + 5,
(bubbleFragment_width * 3) - 25,
(bubbleFragment_height * 2) + labelHeight - 10);
messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];
messageLabel.textAlignment = UITextAlignmentCenter;
messageLabel.textColor = [UIColor darkTextColor];
messageLabel.numberOfLines = 0; //---display multiple lines---
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
///// changed ///////
messsageForTheRow = chatMessage.messsage;
messageLabel.text = messsageForTheRow ;
return cell;
////////////////////
} else {
/////////// changed //////////
static NSString *CellIdentifier = @"Cell2";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
////////////////////////////
// If NO, then create the row with the bubble which shows it from me
// If YES, then create the row with the corresponding bubble
//---add this---
//---date---
// dateLabel = [[[UILabel alloc] init] autorelease];
//dateLabel.tag = DATELABEL_TAG;
//[cell.contentView addSubview: dateLabel];
/////////////////// image ////////////////////////////////////////////////////////////
NSURL *avatarUrl = [NSURL URLWithString:
@"http://www.mobyn.com/mobyn_v3/application/upload/profile_images/default.jpg"];
UIImage *avatarImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:avatarUrl]];
UIImageView *avatar = [[UIImageView alloc] initWithImage:avatarImage];
avatar.frame = CGRectMake(255, 23, 60, 60);
[cell.contentView addSubview:avatar];
//[avatarImage release];
//[avatar release];
//////////////////////////////////////////////////////////////////////////////////////
NSLog(@"BBBBBBBBBBBBBBBBB");
//---top left---
imageView_top_left = [[[UIImageView alloc] init] autorelease];
imageView_top_left.tag = IMAGEVIEW_TAG_1;
[cell.contentView addSubview: imageView_top_left];
//---top middle---
imageView_top_middle = [[[UIImageView alloc] init] autorelease];
imageView_top_middle.tag = IMAGEVIEW_TAG_2;
[cell.contentView addSubview: imageView_top_middle];
//---top right---
imageView_top_right = [[[UIImageView alloc] init] autorelease];
imageView_top_right.tag = IMAGEVIEW_TAG_3;
[cell.contentView addSubview: imageView_top_right];
//---middle left---
imageView_middle_left = [[[UIImageView alloc] init] autorelease];
imageView_middle_left.tag = IMAGEVIEW_TAG_4;
[cell.contentView addSubview: imageView_middle_left];
//---middle middle---
imageView_middle_middle = [[[UIImageView alloc] init] autorelease];
imageView_middle_middle.tag = IMAGEVIEW_TAG_5;
[cell.contentView addSubview: imageView_middle_middle];
//---middle right---
imageView_middle_right = [[[UIImageView alloc] init] autorelease];
imageView_middle_right.tag = IMAGEVIEW_TAG_6;
[cell.contentView addSubview: imageView_middle_right];
//---bottom left---
imageView_bottom_left = [[[UIImageView alloc] init] autorelease];
imageView_bottom_left.tag = IMAGEVIEW_TAG_7;
[cell.contentView addSubview: imageView_bottom_left];
//---bottom middle---
imageView_bottom_middle = [[[UIImageView alloc] init] autorelease];
imageView_bottom_middle.tag = IMAGEVIEW_TAG_8;
[cell.contentView addSubview: imageView_bottom_middle];
//---bottom right---
imageView_bottom_right = [[[UIImageView alloc] init] autorelease];
imageView_bottom_right.tag = IMAGEVIEW_TAG_9;
[cell.contentView addSubview: imageView_bottom_right];
//---message---
messageLabel = [[[UILabel alloc] init] autorelease];
messageLabel.tag = MESSAGELABEL_TAG;
[cell.contentView addSubview: messageLabel];
//---set the images to display for each UIImageView---
imageView_top_left.image =
[UIImage imageNamed:@"bubble_top_left.png"];
imageView_top_middle.image =
[UIImage imageNamed:@"bubble_top_middle.png"];
imageView_top_right.image =
[UIImage imageNamed:@"bubble_top_right.png"];
imageView_middle_left.image =
[UIImage imageNamed:@"bubble_middle_left.png"];
imageView_middle_middle.image =
[UIImage imageNamed:@"bubble_middle_middle.png"];
imageView_middle_right.image =
[UIImage imageNamed:@"bubble_middle_right.png"];
imageView_bottom_left.image =
[UIImage imageNamed:@"bubble_bottom_left.png"];
imageView_bottom_middle.image =
[UIImage imageNamed:@"bubble_bottom_middle.png"];
imageView_bottom_right.image =
[UIImage imageNamed:@"bubble_bottom_right.png"];
/////////////////////////////////////////////////
//---calculate the height for the label---
int labelHeight = [self labelHeight:messsageForTheRow];
labelHeight -= bubbleFragment_height;
if (labelHeight<0) labelHeight = 0;
//---you can customize the look and feel for the date for each message here---
dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0);
dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor];
//---top left---
imageView_top_left.frame =
CGRectMake(bubble_x2, bubble_y, bubbleFragment_width, bubbleFragment_height);
//---top middle---
imageView_top_middle.frame =
CGRectMake(bubble_x2 + bubbleFragment_width, bubble_y2,
bubbleFragment_width, bubbleFragment_height);
//---top right---
imageView_top_right.frame =
CGRectMake(bubble_x2 + (bubbleFragment_width * 2), bubble_y2,
bubbleFragment_width, bubbleFragment_height);
//---middle left---
imageView_middle_left.frame =
CGRectMake(bubble_x2, bubble_y2 + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle middle---
imageView_middle_middle.frame =
CGRectMake(bubble_x2 + bubbleFragment_width, bubble_y2 + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle right---
imageView_middle_right.frame =
CGRectMake(bubble_x2 + (bubbleFragment_width * 2),
bubble_y2 + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---bottom left---
imageView_bottom_left.frame =
CGRectMake(bubble_x2, bubble_y2 + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---bottom middle---
imageView_bottom_middle.frame =
CGRectMake(bubble_x2 + bubbleFragment_width,
bubble_y2 + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height);
//---bottom right---
imageView_bottom_right.frame =
CGRectMake(bubble_x2 + (bubbleFragment_width * 2),
bubble_y2 + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---you can customize the look and feel for each message here---
messageLabel.frame =
CGRectMake(bubble_x2 + 10, bubble_y2 + 5,
(bubbleFragment_width * 3) - 25,
(bubbleFragment_height * 2) + labelHeight - 10);
messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];
messageLabel.textAlignment = UITextAlignmentCenter;
messageLabel.textColor = [UIColor darkTextColor];
messageLabel.numberOfLines = 0; //---display multiple lines---
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
///// changed ///////
messsageForTheRow = chatMessage.messsage;
messageLabel.text = messsageForTheRow ;
return cell;
////////////////////
}
// Then set the data
messsageForTheRow = chatMessage.messsage;
messageLabel.text = messsageForTheRow ;
//--------------
/////////// changed //////////
UITableViewCell *cell = [UITableViewCell alloc];
return cell;
}
答案 0 :(得分:0)
您可能正在cellForRowAtIndexPath
重新调用chatMessage初始化/分配。
默认情况下,UITableView
会在向上/向下滚动时释放屏幕外单元格数据。当您向后滚动时,它会再次调用cellForRowAtIndexPath
。
尝试:
cellForRowAtIndexPath
您的应用应该在cellForRowAtIndexPath
处中断。
我的猜测是你一直打电话给UITableViewCell *cell = [UITableViewCell alloc];
。
每当您向上和向下滚动UITableView
来电cellForRowAtIndexPath
时,最终会重新调用小区分配,因此会有多个图像。
答案 1 :(得分:0)
您需要从IF语句中输出此代码:
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
在之后移动:
//---add this---
UILabel* dateLabel = nil;
UILabel* messageLabel = nil;
UIImageView *imageView_top_left = nil;
UIImageView *imageView_top_middle = nil;
UIImageView *imageView_top_right = nil;
UIImageView *imageView_middle_left = nil;
UIImageView *imageView_middle_right = nil;
UIImageView *imageView_middle_middle = nil;
UIImageView *imageView_bottom_left = nil;
UIImageView *imageView_bottom_middle = nil;
UIImageView *imageView_bottom_right = nil;
希望它能解决你的问题