我从不同大小的 URL 获取图像。我已经根据图像高度设置了单元格高度。
[cell.imgshow setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImageUrl]] placeholderImage:[UIImage imageNamed"preloader_img_1.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.imgshow.image = image;
imageStatus =@"Load";
if (image != nil) {
// [images insertObject:image atIndex:indexPath.row];
[images setObject:image forKey:[NSString stringWithFormat"%li,%li",(long)indexPath.row,(long)indexPath.section]];
NSLog(@"%@",images);
[tblDropDown reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
这是我用于图像加载和引用代码的代码。
-(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
UIImage *image = (UIImage *)[images objectForKey:
[NSString stringWithFormat"%li,%li",
(long)indexPath.row,(long)indexPath.section]];
if (image != nil)
{
return image.size.height+300;
}
else{
return 300.0;
}
}
Best Answer-推荐答案 strong>
希望对你有所帮助。
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)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;
}
欲了解更多信息,请参阅以下链接。
https://github.com/jurezove/dynamic-uitableviewcells/tree/master
关于ios - 如何根据图像大小设置高度单元格,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/32443094/
|