我正在加載特定單元格的圖像集,並且只有在等待所有圖像加載後才能正常工作,我正在使用線程從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。 –