【问题标题】:Adding UIImageView to UITableview conditionally using SDWebImage使用 SDWebImage 有条件地将 UIImageView 添加到 UITableview
【发布时间】:2014-02-18 20:46:31
【问题描述】:

我正在使用 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;
}

我需要你的大力帮助。

【问题讨论】:

  • 正如@Putz 提到的理想情况下,您应该使用自定义 TableCell 类,但是在单元格的 contentView 上添加 UIImageView 子视图并使用标签检索就可以了。

标签: ios uitableview reusability sdwebimage


【解决方案1】:

感谢大家的建议。我创建了一个名为 CustomClass.h/.m 的 UITableViewCell 子类,并创建了 CusstomCell.xib,然后将 UITableViewCell 对象添加到 nib 文件中,并将 UIImageView 添加到单元格中。

@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;

}

【讨论】:

    【解决方案2】:

    您可能必须继承 UITableViewCell 并将imgView 添加为类变量。然后在您的dequeueReusableCellWithIdentifier 中,您可以检查返回的单元格以查看imgView 是否已经存在。然后,您可以根据您的检查更新或删除 imgView。

    正如您现在所拥有的那样,您正在向单元格的 contentView 添加一个 imageView。然后重新使用该单元格(其中仍然包含旧的 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;
    }
    

    因此,如果图像已加载到单元格中,则将其删除,然后担心以与当前相同的方式重新加载它。这使得如果您想要更新图像,您可以轻松处理它以进行修改。

    【讨论】:

    • 感谢您的帮助,您能否给我看一下您的解决方案的示例代码?
    • @user1887971 更新了一些代码。你应该可以从那里拿走它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多