簡體   English   中英

使用SDWebImage有條件地將UIImageView添加到UITableview

[英]Adding UIImageView to UITableview conditionally using SDWebImage

我正在使用SDWebImage庫在UITableView中下載UIImageView的圖像。 我的tableview的內容是在viewDidLoad中初始化的數組,如下所示:

- (void)viewDidLoad
{
[super viewDidLoad];
self.myArray =@[@"http://i.imgur.com/CUwy8ME.jpg",@"http://i.imgur.com/lQRlubz.jpg",@"AlAhmed",@"Welcome",@"jfhskjh",@"hfyyu",@"lkdjfs",@"mfjsoi",@"jsdkjhdksjh",@"ljsdkfhuhs"];

[self.tableView setRowHeight:100];

[self.tableView reloadData];

}

我的想法是檢測myArray中是否存在URL,從而在該單元格中下載圖像。 我的代碼工作正常,但是圖像顯示在其他單元格中(我認為是重復使用單元格),但是我無法解決問題。

這是tableview委托的代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:           (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Celll";
UITableViewCell *cell = (UITableViewCell *)[tableView       dequeueReusableCellWithIdentifier:CellIdentifier];

UIImageView *imgView=nil;
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)];
    [cell.contentView addSubview:imgView];
}


NSError *error;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];

NSString *myString = [self.myArray objectAtIndex:indexPath.row];
NSArray *matches = [detector matchesInString:myString
                                     options:0
                                       range:NSMakeRange(0, [myString length])];

for (NSTextCheckingResult *match in matches) {
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];

        [imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"120.png"] completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
        }];
    }

}


[ cell.textLabel setText:[self.myArray objectAtIndex:indexPath.row]];


return cell;
}

請給我很大的幫助。

您可能必須將UITableViewCell子類化,並將imgView添加為類變量。 然后,在您的dequeueReusableCellWithIdentifier您可以檢查返回的單元格以查看imgView已經存在。 然后,您可以根據檢查結果更新或刪除imgView。

現在,您已經將imageView添加到單元格的contentView中。 然后,該單元被重新使用(仍然包含舊的imageView),然后添加另一個imageView。 這將很快成為記憶的噩夢。

編輯:現在與代碼! (脂肪減少了33%!)

@interface CustomCell : UITableViewCell

@property (nonatomic, strong) UIImageView *imageView;

@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:           (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Celll";
UITableViewCell *cell = (UITableViewCell *)[tableView       dequeueReusableCellWithIdentifier:CellIdentifier];

UIImageView *imgView=nil;
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if(cell.imageView)
{
    cell.imageView.image = nil;
}
else
{
    cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)];
    [cell.contentView addSubview:cell.imageView];
}


NSError *error;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];

NSString *myString = [self.myArray objectAtIndex:indexPath.row];
NSArray *matches = [detector matchesInString:myString
                                     options:0
                                       range:NSMakeRange(0, [myString length])];

for (NSTextCheckingResult *match in matches) {
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];

        [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"120.png"] completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
        }];
    }

}


[ cell.textLabel setText:[self.myArray objectAtIndex:indexPath.row]];


return cell;
}

因此,如果圖像已經加載到單元中,則可以將其刪除,然后再擔心以與當前相同的方式重新加載它。 這樣一來,如果您想更新圖像,就可以輕松進行修改。

謝謝你們的建議。 我創建了一個名為CustomClass.h / .m的UITableViewCell子類,並創建了CusstomCell.xib,然后將UITableViewCell對象添加到了nib文件中,並將UIImageView添加到了Cell中。

@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

然后,我按如下方式編輯了代碼,它可以正常工作。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
 static NSString *customCellIdentifier = @"MyCell";
CustomCell *myCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if (myCell == nil) {
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
    myCell = [nib lastObject];
}

   NSURL *url =[self LookForURLinString:[self.myArray objectAtIndex:indexPath.row]];
if (url) {
    [myCell.myImageView setImageWithURL:url
                       placeholderImage:[UIImage imageNamed:@"120.png"]
                              completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){}];
}
else {
    [myCell.myImageView setImage:nil];
    [myCell.myImageView removeFromSuperview];
}
[myCell.textLabel setText:[self.myArray objectAtIndex:indexPath.row]];
return myCell;


}
-(NSURL *)LookForURLinString:(NSString *)string
{
NSError *error;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];



NSArray *matches = [detector matchesInString:string
                                     options:0
                                       range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
        return url;
    }
}
return nil;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM