【发布时间】:2015-03-17 21:29:26
【问题描述】:
我有一个带有自定义单元格的表格视图。每个单元格都有一个图像、标题和描述。当我第一次加载表格时,它加载得很好。如果我慢慢滚动浏览图像,似乎也可以正常工作。一旦我快速向下滚动(假设单元格的数量足够大,如果不上下滚动就无法容纳)图像开始以随机顺序更改单元格。有些细胞有两次相同的图像。
知道为什么会这样吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
BookmarkCellViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NewsArticle *currArt = [self.lN_Dept objectAtIndex:indexPath.row];
if(currArt.artImage == Nil)
{
if([currArt.mainImage_URL rangeOfString:@"<img src="].location != NSNotFound)
{
NSRange range = [currArt.mainImage_URL rangeOfString:@"<img src=\"/CONC/"];
NSString *substring = [[currArt.mainImage_URL substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSRange range2 = [substring rangeOfString:@"\""];
NSString *substring2 = [[substring substringToIndex:NSMaxRange(range2)-1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *imageURL = [PUB_URL stringByAppendingString:substring2];
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
currArt.artImage = image;
cell.ArtDisplayImage.image = currArt.artImage;
}
}];
}
}
else
{
cell.ArtDisplayImage.image = currArt.artImage;
}
[cell configureCellForEntry:currArticle];
return cell;
}
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *Data, NSError *error) {
if ( !error )
{
UIImage *image = [[UIImage alloc] initWithData:Data];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}
}];
}
【问题讨论】:
-
在这里看看我的回答:stackoverflow.com/questions/7852033/…
-
@hypercrypt 你能举个例子,你会如何用上面的代码来做?
标签: ios uitableview