我正在尝试在驻留在表格视图单元格中的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;
}
}
答案 0 :(得分:2)
您从另一个单元格看到图像的原因是为了提高效率,表格单元格可以重复使用。要解决此问题,请在nil
中将可重复使用的单元格出列后将图像设置为tableView:cellForRowAtIndexPath:
。
答案 1 :(得分:2)
首先,你不应该在cellForRowAtIndexPath中完成所有这些工作。
你应该将UITableViewCell子类化,并将单元格布置在它自己的类中。
indexPath处的行的单元格应加载单元格并填充数据。不是布局加载控件。
其次,一旦你完成了这个,你可以打电话给...
- (void)prepareForReuse
{
self.imageView.image = nil;
}
这将清除单元格出列时的imageView,这样如果有延迟加载图像,那么你只能看到一个空白的imageView而不是之前的图像。