【发布时间】: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