UITableView 中的 UIImage 显示另一个单元格的图像一秒钟
Posted
技术标签:
【中文标题】UITableView 中的 UIImage 显示另一个单元格的图像一秒钟【英文标题】:UIImage in UITableView displaying the other cell's image for a second 【发布时间】:2013-05-13 20:53:08 【问题描述】:我正在尝试在位于表格视图单元格中的 UIImage 中异步加载图像。有时它会先显示另一个单元格的图像,然后才会显示正确的图像。
这是我正在使用的代码
[[TMCache sharedCache] objectForKey:post[@"gif"] block:^(TMCache *cache, NSString *key, id object)
if (object)
if ([self isRowZeroVisible:indexPath.section])
[GIFLoader loadGIFData:object to:postGif for:feedTableView];
return;
dispatch_async(dispatch_get_main_queue(), ^
__block NSURL* url = [NSURL URLWithString:post[@"gif"]];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
[loader startRequestWithResponseHandler:nil
progress:nil
completion:^(NSData* receivedData, NSInteger httpStatusCode)
if ([self isRowZeroVisible:indexPath.section])
[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView];
[[TMCache sharedCache] setObject:receivedData forKey:post[@"gif"]];
errorHandler:nil];
);
];
[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView]; is a method I created that loads GIFs in a uiimageview. it does that in a background thread and then assigns the Image in the main thread though.
对于异步加载,我需要遵循某些做法吗?
我不确定我的代码有什么问题。任何帮助将不胜感激
编辑
//Loading stuff into tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@autoreleasepool
//Type of Cells
static NSString *simpleTableImageIdentifier = @"gifCell";
static NSString *simpleTableBodyIdentifier = @"bodyCell";
static NSString *simpleTableActionsIdentifier = @"actionsCell";
UITableViewCell *cell = nil;
//Data that goes in the cell
NSDictionary *post = [dataArray objectAtIndex:indexPath.section];
//User info
UIImageView *userAvatar;
UILabel *postUserFullNameLabel;
UILabel *postUsername;
//GIF Date
OHAttributedLabel* gifDate = nil;
NSMutableAttributedString* gifDateString;
//Feed GIF
NSString *gifBody = [[NSString alloc]init];
OHAttributedLabel* attrLabel = nil;
NSMutableAttributedString* mas;
//Location
OHAttributedLabel* gifLocation = nil;
NSMutableAttributedString* gifLocationString;
UILabel *locationBg;
//Buttons
UIButton *btn1 =[[UIButton alloc] initWithFrame:CGRectMake(11,0,50,30)];
UIButton *btn2 =[[UIButton alloc] initWithFrame:CGRectMake(69,0,50,30)];
UIButton *btn3 =[[UIButton alloc] initWithFrame:CGRectMake(259,0,50,30)];
if (indexPath.row == 0)
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableImageIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableImageIdentifier];
__block UIImageView *postGif= (UIImageView *)[cell viewWithTag:104];
postGif.image = nil;
[[TMCache sharedCache] objectForKey:post[@"gif"] block:^(TMCache *cache, NSString *key, id object)
if (object)
if ([self isRowZeroVisible:indexPath.section])
[GIFLoader loadGIFData:object to:postGif for:feedTableView];
return;
dispatch_async(dispatch_get_main_queue(), ^
__block NSURL* url = [NSURL URLWithString:post[@"gif"]];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
[loader startRequestWithResponseHandler:nil
progress:nil
completion:^(NSData* receivedData, NSInteger httpStatusCode)
if ([self isRowZeroVisible:indexPath.section])
[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView];
[[TMCache sharedCache] setObject:receivedData forKey:post[@"gif"]];
errorHandler:nil];
);
];
postGif.layer.cornerRadius = 2.0;
postGif.layer.masksToBounds = YES;
postGif.clipsToBounds = YES;
locationBg = (UILabel *)[cell viewWithTag:106];
userAvatar = (UIImageView *)[cell viewWithTag:100];
if ([post [@"user_avatar"] length ] > 0)
NSString *img = [@"https://d1f7i732a4e7fw.cloudfront.net/" stringByAppendingString:post [@"user_avatar"]];
[userAvatar setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:@"userDefaultPicture.png"]];
else
userAvatar.image = [UIImage imageNamed:@"userDefaultPicture.png"];
userAvatar.layer.cornerRadius = 18.0;
userAvatar.layer.borderWidth = 1.0;
userAvatar.layer.borderColor = (__bridge CGColorRef)([UIColor lightGrayColor]);
userAvatar.clipsToBounds = YES;
postUserFullNameLabel = (UILabel *)[cell viewWithTag:101];
postUserFullNameLabel.text = post[@"user_fullname"];
postUsername = (UILabel *)[cell viewWithTag:102];
postUsername.text = [NSString stringWithFormat:@"@%@",post[@"user_username"]];
gifDate = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(246,9,60,21)];
gifDate.autoresizingMask = UIViewAutoresizingNone;
gifDate.centerVertically = YES;
gifDate.highlightedTextColor = [UIColor whiteColor];
gifDate.tag = 103;
gifDate.backgroundColor = [UIColor clearColor];
gifDate.extendBottomToFit = NO;
gifDateString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:@"\ue003 %@",post[@"date"]]];
[gifDateString setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0]];
[gifDateString setFont:[UIFont fontWithName:@"icomoon" size:10.0] range:NSMakeRange(0,1)];
[gifDateString setTextColor:[UIColor whiteColor]];
[gifDateString setTextAlignment:kCTTextAlignmentRight lineBreakMode:kCTLineBreakByTruncatingTail];
[OHASBasicMarkupParser processMarkupInAttributedString:gifDateString];
gifDate.attributedText = gifDateString;
if (!(post[@"latitude"] == (id)[NSNull null]))
//Location of the Post
gifLocation = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(165,27,141,21)];
gifLocation.autoresizingMask = UIViewAutoresizingNone;
gifLocation.centerVertically = YES;
gifLocation.highlightedTextColor = [UIColor whiteColor];
gifLocation.tag = 107;
gifLocation.backgroundColor = [UIColor clearColor];
gifLocation.extendBottomToFit = NO;
gifLocationString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:@"\uf041 %@",post[@"locationName"]]];
[gifLocationString setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0]];
[gifLocationString setFont:[UIFont fontWithName:@"icomoon" size:12.0] range:NSMakeRange(0,1)];
[gifLocationString setTextColor:[UIColor whiteColor]];
[gifLocationString setTextAlignment:kCTTextAlignmentRight lineBreakMode:kCTLineBreakByTruncatingTail];
[OHASBasicMarkupParser processMarkupInAttributedString:gifLocationString];
gifLocation.attributedText = gifLocationString;
else if (indexPath.row == 1)
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableBodyIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableBodyIdentifier];
//Body of GIF
// Add more from here http://www.easyapns.com/iphone-emoji-alerts
gifBody = post[@"body"];
attrLabel = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(10,5,kLabelWidth,tableView.rowHeight-2*kLabelVMargin)];
attrLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
attrLabel.centerVertically = NO;
attrLabel.automaticallyAddLinksForType = NSTextCheckingAllTypes;
attrLabel.delegate = self;
attrLabel.highlightedTextColor = UIColorFromRGB(0x333333);
attrLabel.tag = kAttributedLabelTag;
attrLabel.backgroundColor = [UIColor clearColor];
attrLabel.extendBottomToFit = YES;
[cell.contentView addSubview:attrLabel];
attrLabel = (OHAttributedLabel*)[cell viewWithTag:kAttributedLabelTag];
mas = [NSMutableAttributedString attributedStringWithString:gifBody];
[mas setFont:[UIFont fontWithName:@"Helvetica Neue" size:14.0]];
[mas setTextColor:UIColorFromRGB(0x333333)];
[mas setTextAlignment:kCTTextAlignmentLeft lineBreakMode:kCTLineBreakByWordWrapping];
[OHASBasicMarkupParser processMarkupInAttributedString:mas];
attrLabel.attributedText = mas;
else if (indexPath.row == 2)
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableActionsIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableActionsIdentifier];
[self customizeButtons:btn1];
btn1.tag = (indexPath.section +1)*200;
btn1.titleLabel.font = [UIFont fontWithName:@"icomoon" size:16.0];
if ([post[@"is_Favoring"] boolValue])
[btn1 setTitleColor:UIColorFromRGB(0xE4717A) forState:UIControlStateNormal];
else
[btn1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[btn1 setTitle:@"\uf004" forState:UIControlStateNormal];
btn1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn1 addTarget:self
action:@selector(favorAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn1];
[self customizeButtons:btn2];
btn2.tag = (indexPath.section +1)*2000;
btn2.titleLabel.font = [UIFont fontWithName:@"icomoon" size:16.0];
[btn2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[btn2 setTitle:@"\ue000" forState:UIControlStateNormal];
[cell.contentView addSubview:btn2];
btn2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn2 addTarget:self
action:@selector(commentAction:)
forControlEvents:UIControlEventTouchUpInside];
[self customizeButtons:btn3];
btn3.titleLabel.font = [UIFont fontWithName:@"icomoon" size:16.0];
[btn3 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[btn3 setTitle:@"\ue001" forState:UIControlStateNormal];
btn3.tag = (indexPath.section +1)*20000;
[cell.contentView addSubview:btn3];
btn3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn3 addTarget:self
action:@selector(otherAction:)
forControlEvents:UIControlEventTouchUpInside];
//Paginiation
if (indexPath.section == [dataArray count]-4 && indexPath.row == 1)
[self loadNextPage];
return cell;
【问题讨论】:
【参考方案1】:您看到来自另一个单元格的图像的原因是为了提高效率,表格单元格被重复使用。要解决此问题,请在将 tableView:cellForRowAtIndexPath:
中的可重用单元出列后将您的图像设置为 nil
。
【讨论】:
vacawama,是这样的吗?__block UIImageView *postGif= (UIImageView *)[cell viewWithTag:104]; postGif.image = nil;
我不确定这是否是您的问题,但我看到您在各个位置的单元格上调用 viewWithTag
,然后再次将该视图作为子视图添加到单元格中。如果它已经是单元格的一部分,为什么要再次添加它。此外,由于单元格被重复使用,如果您不删除这些额外的子视图,它们只会累积。您是完全在原型单元格中使用 Image Builder,还是完全在代码中执行此操作?
vacawama,我正在使用故事板,让我修复 viewWithTag 问题,看看是否有影响
vacawama,即使这样也不能解决问题
更新编辑以显示您的最新代码。请描述您当前看到的行为。【参考方案2】:
首先,您不应该在 cellForRowAtIndexPath 中做所有这些事情。
你应该继承 UITableViewCell 并将单元格布局在它自己的类中。
indexPath 处的行单元格应加载单元格并填充数据。不是布局负载的控件。
其次,完成此操作后,您可以调用...
- (void)prepareForReuse
self.imageView.image = nil;
这将在单元格出队时清除 imageView,这样如果加载 hte 图像有延迟,那么您只会看到一个空白的 imageView,而不是之前的图像。
【讨论】:
Fogmeister,你能详细解释一下吗? 明天更新。我现在要睡觉了。不过现在,看看这里的“子类化”标题...mobile.tutsplus.com/tutorials/iphone/… 雾迈斯特,感谢您的链接。我也试过了,没用以上是关于UITableView 中的 UIImage 显示另一个单元格的图像一秒钟的主要内容,如果未能解决你的问题,请参考以下文章
UIImage 作为 UITableViewCell 中的辅助视图未显示
如何在 uitableviewcell 上显示 UIImage [关闭]