Ios 目标c中具有FIRStorage的SDWebImage

Ios 目标c中具有FIRStorage的SDWebImage,ios,objective-c,uitableview,asynchronous,sdwebimage,Ios,Objective C,Uitableview,Asynchronous,Sdwebimage,我有一张从FIRebase存储中获取图像的照片。但是当我在表格视图中上下滚动时,它正在改变图像。我在互联网上尝试了所有的东西,堆栈溢出,但没有成功。下面是我的代码 #import "TableViewController.h" #import "CustomCell.h" #import "DetailViewController.h" #import "dataObj.h" #import "MZFormSheetPresentationViewControllerSegue.h" #impo

我有一张从FIRebase存储中获取图像的照片。但是当我在表格视图中上下滚动时,它正在改变图像。我在互联网上尝试了所有的东西,堆栈溢出,但没有成功。下面是我的代码

#import "TableViewController.h"
#import "CustomCell.h"
#import "DetailViewController.h"
#import "dataObj.h"
#import "MZFormSheetPresentationViewControllerSegue.h"
#import "BrowseTableViewController.h"
#import "FavouritesTableViewController.h"
#import "ProfileObj.h"
#import "LoginViewController.h"
#import "InboxTableViewController.h"
#import "ProfileViewController.h"

#import <KeychainItemWrapper/KeychainItemWrapper.h>

@import SDWebImage;
@import FirebaseDatabaseUI;
@import Firebase;
@import FirebaseAuthUI;
@import FirebaseGoogleAuthUI;
@import FirebaseFacebookAuthUI;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier;
    CustomCell *cell;
    if(indexPath.row == 0){
        cellIdentifier = @"Cell1";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    }else{

    cellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
FIRStorage *storage = [FIRStorage storage];
    FIRStorageReference *storageRef = [[storage reference] child:@"publicListings"];

    if([photo0 intValue] == 1){
        FIRStorageReference *idRef = [storageRef child:self.snaps.key];
        FIRStorageReference *folderRef = [idRef child:@"photos"];
        NSString *fileName = @"photo0.jpg";
        FIRStorageReference *imageRef = [folderRef child:fileName];
        [imageRef downloadURLWithCompletion:^(NSURL *URL, NSError *error){
            if(error != nil){
                NSLog(@"%@",error);
            }
            else{
                [cell.imgPhoto sd_setImageWithURL:URL
                             placeholderImage:[UIImage imageNamed:@"image3.png"]
                                      options:SDWebImageRefreshCached];

//                NSString *url = URL.absoluteString;
//                [cell.imgPhoto sd_setImageWithURL:[NSURL URLWithString:url]];
            }
        }];
    }
}
}
#导入“TableViewController.h”
#导入“CustomCell.h”
#导入“DetailViewController.h”
#导入“dataObj.h”
#导入“MZFormSheetPresentationViewControllerSegue.h”
#导入“BrowseTableViewController.h”
#导入“FavoriteStableViewController.h”
#导入“ProfileObj.h”
#导入“LoginViewController.h”
#导入“InboxTableViewController.h”
#导入“ProfileViewController.h”
#进口
@导入SDWebImage;
@导入FirebaseDatabaseUI;
@进口火基;
@导入FirebaseAuthUI;
@导入FirebaseGoogleAuthUI;
@导入FirebaseFacebookAuthUI;
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*cellIdentifier;
自定义单元格*单元格;
if(indexPath.row==0){
cellIdentifier=@“Cell1”;
cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}否则{
cellIdentifier=@“单元”;
cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
FIRStorage*存储=[FIRStorage存储];
FIRStorageReference*storageRef=[[storage reference]子项:@“publicListings”];
如果([photo0 intValue]==1){
FIRStorageReference*idRef=[storageRef子项:self.snaps.key];
FIRStorageReference*folderRef=[idRef子项:@“照片”];
NSString*文件名=@“photo0.jpg”;
FIRStorageReference*imageRef=[folderRef子项:文件名];
[imageRef downloadURLWithCompletion:^(NSURL*URL,NSError*error){
如果(错误!=nil){
NSLog(@“%@”,错误);
}
否则{
[cell.imgPhoto sd_setImageWithURL:URL
占位符图像:[UIImage ImageName:@“image3.png”]
选项:SDWebImageRefreshCached];
//NSString*url=url.absoluteString;
//[cell.imgPhoto sd_setImageWithURL:[NSURL URLWithString:url]];
}
}];
}
}
}

TableViewCell是可重用的,您可能会得到一个已使用的已退出队列的单元格,请在获得单元格后尝试清除单元格状态

在您的情况下,请尝试设置

cell.imgPhoto = nil; 


在使用此单元格之前

我应该把上面的代码行放在哪里?就在[cell.imgPhoto sd_setImageWithURL:URL占位符图像:[UIImage ImageName:@“image3.png”]选项:SDWebImageRefreshCached]之前;或者在那之后?在你得到一个单元之后,即使如此,你也应该将你的可重用单元清除到初始状态。如果每个照片的url都是唯一的,请尝试删除SDWebImageRefreshCached选项。
cell.imgPhoto = [UIImage imageNamed:@"image3.png"];