Ios 使用SDWebImage有条件地将UIImageView添加到UITableview
我正在使用SDWebImage库在UITableView中下载UIImageView的图像。my tableview的内容是在viewDidLoad中初始化的数组,如下所示:Ios 使用SDWebImage有条件地将UIImageView添加到UITableview,ios,uitableview,reusability,sdwebimage,Ios,Uitableview,Reusability,Sdwebimage,我正在使用SDWebImage库在UITableView中下载UIImageView的图像。my tableview的内容是在viewDidLoad中初始化的数组,如下所示: - (void)viewDidLoad { [super viewDidLoad]; self.myArray =@[@"http://i.imgur.com/CUwy8ME.jpg",@"http://i.imgur.com/lQRlubz.jpg",@"AlAhmed",@"Welcome",@"jfhskjh",@"
- (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子类,创建了customCell.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;
}
谢谢你的帮助,你能给我看一下你的解决方案的示例代码吗?@user1887971更新了一些代码。正如@Putz所提到的,理想情况下,您应该使用自定义TableCell类,但是在单元格的contentView上添加UIImageView子视图并使用标记进行检索就可以了。