我正在加载特定单元格的图像集,并且只有在等待所有图像加载后才能正常工作,我正在使用线程从Web获取图像问题出在何时在加载过程仍在进行时,我滚动表格视图,它会在错误的单元格位置生成图像。我知道这是错误的位置,因为我也为每个图像集设置了一个标签。任何帮助,将不胜感激提前感谢!滚动时在错误的UITableViewCell上加载图像
这里是示例代码。
- (void)viewDidLoad{
[super viewDidLoad];
/* loads xml from the web and parses the images and store it in core data(sqlite)
it uses NSFetchedController protocol for inserting cell after saving into core data
*/
[self loadFromXmlSource];
}
我有3个部分,使用故事板中的3个不同的原型单元格,我遇到的问题是照片部分。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Media *mediaModel = [_fetchedResultsController objectAtIndexPath:indexPath];
NSString *CellIdentifier = @"MediaCellNoImage";
if ([[mediaModel.category lowercaseString] isEqualToString:@"photos"]) {
CellIdentifier = @"MediaGalleryCell";
} else if ([[mediaModel.category lowercaseString] isEqualToString:@"videos"]) {
CellIdentifier = @"MediaCellNoImage";
} else if ([[mediaModel.category lowercaseString] isEqualToString:@"podcasts"]) {
CellIdentifier = @"MediaCell";
}
MediaGalleryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MediaGalleryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// I am also having a problem with image duplication so I always delete all images
if([cell.reuseIdentifier isEqualToString:@"MediaGalleryCell"]) {
for(id subview in cell.scrollView.subviews){
[subview removeFromSuperview];
}
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
这里是配置电池,我产生一个线程用于图像下载
-(void)configureCell:(MediaGalleryCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Media *mediaModel = [_fetchedResultsController objectAtIndexPath:indexPath];
if ([[mediaModel.category lowercaseString] isEqualToString:@"photos"]) {
NSEnumerator *e = [mediaModel.attachments objectEnumerator];
Attachment *attachment;
// iterate all images in a particular cell
while ((attachment = [e nextObject])) {
NSDictionary *objects = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
attachment.identifier, @"identifier",
attachment.thumb, @"thumb",
attachment.filename, @"filename",
cell, @"cell",
nil];
// for each image spawn a thread i am also passing the cell in order to set it there.
[NSThread detachNewThreadSelector:@selector(loadImageInScrollView:)
toTarget:self withObject:objects];
}
} else {
// code for other section
}
cell.titleLabel.text = mediaModel.title;
cell.contentLabel.text = mediaModel.content;
}
这里是在线程的方法
-(void)loadImageInScrollView:(NSDictionary *)objects
{
MediaGalleryCell *cell = [objects valueForKey:@"cell"];
SBCacheFile *cacheFile = [self getCache];
NSString *finalIdentifier = [NSString stringWithFormat:@"%@_s", [objects valueForKey:@"identifier"]];
// here is where I fetched the image and cache it locally
UIImage *image = [cacheFile getCachedFileWithUrl:[objects valueForKey:@"thumb"]
withName:[objects valueForKey:@"filename"]
andStringIdentifier:finalIdentifier];
NSDictionary *obj = [[NSDictionary alloc] initWithObjectsAndKeys:
cell, @"cell",
image, @"image",
nil];
//after fetching the image I need to send it back to the main thread in order to do some UI operation
[self performSelectorOnMainThread:@selector(setImageViewForGallery:) withObject:obj waitUntilDone:YES];
}
这里是代码中的最后一块我添加图像作为一个单元格属性的子视图,它是一个scrollView,它像一个单元格中的图像旋转木马一样。
-(void)setImageViewForGallery:(NSDictionary *)dictionary
{
MediaGalleryCell *cell = [dictionary valueForKey:@"cell"];
UIImage *image = [dictionary valueForKey:@"image"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageView *lastImageView =[[cell.scrollView subviews] lastObject];
if (lastImageView != nil) {
[imageView setFrame:CGRectMake((lastImageView.frame.origin.x + lastImageView.frame.size.width + 10.0f),
5.0f, 70.0f, 70.0f)];
} else {
[imageView setFrame:CGRectMake(5.0f, 5.0f, 70.0f, 70.0f)];
}
// This is where I add the imageView
[cell.scrollView addSubview:imageView];
UIImageView *lastViewForSize = [[cell.scrollView subviews] lastObject];
[cell.scrollView setContentSize:CGSizeMake(lastViewForSize.frame.origin.x + lastViewForSize.frame.size.width + 0.5,cell.scrollView.frame.size.height)];
}
谢谢你的答案是非常足智多谋,我想我需要研究这个NSOperationQueue等我是新来的ios。 –