Jur*_*ure 20 height dynamic uitableview ios afnetworking
我正在使用UIImageView+AFNetworking
类别进行异步图像加载.一切正常,但我已经尝试了一些事情,并根据下载的图像调整单元格的高度时没有成功.我希望图像适合单元格的宽度,但调整高度,但我不知道如何实现.我已经尝试重新加载下载图像的行,但这只会导致cellForRowAtIndexPath
再次触发并再次设置所有内容,等等.几乎是一个递归.
我正在计算新的图像大小差异并将其存储NSMutableArray
在UIImageView的成功块中然后重新加载行setImageWithURLRequest:placeholderImage:success:
.
我得到几行的正确高度heightForRowAtIndexPath
然后,表开始表现怪异,一切都覆盖等等.
有任何想法吗?
谢谢!
编辑
我最终使用了Ezeki的答案.我还写了一篇关于这个主题的帖子,并制作了一个使用这种技术的示例应用程序.
在这里查看.
Eze*_*eki 17
您需要将下载的图像存储在内存或磁盘上,因此下次尝试从此URL获取图像时,您将从缓存中收到图像.
所以,如果你这样做,你将不得不做这样的事情:
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
并且您应该在此表视图数据源方法中返回新单元格的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
我建议你使用SDWebImage库,而不是AFNetworking
因为它可以为你缓存你的图像到memcache和磁盘,它很容易使用.因此,如果您决定使用它,您的下载图像代码将如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
success:^(UIImage *image, BOOL cached) {
// save height of an image to some cache
[self.heightsCache setObject:[NSNumber numberWithFloat:imHeight]
forKey:urlKey];
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
failure:^(NSError *error) {... failure code here ...}];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// try to get image height from your own heights cache
// if its is not there return default one
CGFloat height = [[self.heightsCache objectForKey:urlKeyFromModelsArrayForThisCell] floatValue];
...
return newHeight;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12322 次 |
最近记录: |