【发布时间】:2014-01-12 21:29:50
【问题描述】:
我试图将 UIImage 仅添加到 UITableview 中的某些单元格,但是每当用户滚动经过图像时,我最终会得到一堆放错位置的图像视图,图像视图最终会出现在所有错误的单元格中,这只是一个混乱。感谢您提前回复。 代码:
- (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;
}
【问题讨论】:
标签: ios objective-c cocoa-touch uitableview uiimageview