我正在尝试将UIImage添加到UITableview中的某些单元格中,但每当用户滚动浏览图像时,我最终会得到一堆错位的图像视图,imageview最终会出现在所有错误的单元格中,它只是一个一塌糊涂。感谢您提前做出的任何回复。 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"sections:%ld", (long)tableView.numberOfSections);
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *message = self.retreivedMessages[indexPath.row];
NSLog(@"%lu", (long)indexPath.row);
UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:20000];
UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:10000];
UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:30000];
image.tag = indexPath.row;
UILabel *usernameLabel = (UILabel *)[cell.contentView viewWithTag:50000];
UILabel *dateLabel = (UILabel *)[cell.contentView viewWithTag:60000];
UITapGestureRecognizer *tapPhoto = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedPhoto:)];
CGRect imageFrame = image.frame;
CGPoint imagePoint = imageFrame.origin;
imagePoint.y = 8;
image.frame = imageFrame;
NSLog(@"frameY: %f", imageFrame.origin.y);
NSLog(@"imageFrameOrigin: %f", image.frame.origin.y);
UIButton *replyButton = (UIButton *)[cell.contentView viewWithTag:40000];
replyButton.tag = indexPath.row;
messageContent.text = [message objectForKey:@"messageContent"];
NSString *content = [message objectForKey:@"messageContent"];
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]};
CGRect rect = [messageContent.text boundingRectWithSize:CGSizeMake(251, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
messageContent.font = [UIFont systemFontOfSize:17.0f];
CGFloat height = ceilf(rect.size.height);
messageContent.frame = CGRectMake(40, 100, 251, height);
messageContent.lineBreakMode = NSLineBreakByWordWrapping;
NSLog(@"Message: %@", content);
UIImageView *photoView = (UIImageView *)[cell.contentView viewWithTag:70000];
NSLog(@"boolean photo: %@", [message objectForKey:@"hasPhoto"]);
if ([[message objectForKey:@"hasPhoto"] isEqualToString:@"YES"]) {
PFFile *photoFile = [message objectForKey:@"photo"];
[self addPhotoToCell:cell file:photoFile photoView:photoView];
[photoView addGestureRecognizer:tapPhoto];
NSLog(@"added");
} else {
}
NSLog(@"Height:%f", messageContent.frame.size.height);
NSLog(@"y: %f", messageContent.frame.origin.y);
NSLog(@"estimated height: %f", height);
NSLog(@"imageY: %f", image.frame.origin.y);
NSLog(@"ID: %@", [message objectForKey:@"ID"]);
usernameLabel.text = [message objectForKey:@"senderSetName"];
nameLabel.text = [message objectForKey:@"senderName"];
PFFile *photoProfile = [message objectForKey:@"senderPicture"];
NSData *data = [photoProfile getData];
UIImage *profileImage = [UIImage imageWithData:data];
image.image = profileImage;
image.layer.cornerRadius = 27.0;
image.layer.masksToBounds = YES;
self.messageDate = [message objectForKey:@"date"];
NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
[formatDate setDateFormat:@"MM/dd 'at' hh:mm a"];
NSString *dateString = [formatDate stringFromDate:self.messageDate];
dateLabel.text = dateString;
return cell;
}
答案 0 :(得分:1)
如果是“照片”视图,您只想在某些情况下显示,那么我认为您要在else
块中添加一些代码,以隐藏或删除单元格中的任何照片视图。
问题是,一旦你开始滚动,表视图将开始重用一些滚动屏幕的单元格来填充滚动到视图中的新单元格。如果其中一个单元格中有可见的照片(通过if
语句的真正分支,您正在创建照片视图并调用addPhotoToCell
),那么dequeueReusableCellWithIdentifier:
可能会返回其中一个单元格所有的观点完好无损,而不是返回一个全新的未初始化的细胞。如果您最终重复使用其中一个单元格作为hasPhoto
不是YES的消息行,那么看起来您没有做任何事情来隐藏该照片视图。因此,请向else
块添加一些代码,以隐藏该照片视图(如果存在)。
答案 1 :(得分:1)
删除此行,因为它将重用用户滚动后释放的单元格。假设滚动的单元格是否具有UIImageView并且该单元格可能被重用(这会导致问题)。
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
而是每次都创建一个单元格。它会很棒。
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
如果这会使你的应用程序变慢,请使用if else条件检查释放的单元格是否有cellImage并将其删除。
答案 2 :(得分:0)
你的做法是错误的。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"1"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];
UIImageView *cellImage = [UIImageView.alloc initWithFrame:CGRectMake(10, 5, 35 , 35)];
cellImage.tag = 1 ;
[cellImage setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:cellImage];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
UIImageView*cellImage = (UIImageView*)[cell.contentView viewWithTag:1];
[cellImage setImage:[UIImage imageNamed:@"ratingFullIcon.png"]];
return cell;
}
你应该分配UITableViewCell: 当单元格不存在时,也应该分配UITableViewCell contentView中的所有子视图。如果它存在,我们不需要分配init。
setText或setImage超出此条件。