Iphone 动态增加表格单元格的高度
我搜索表格单元格的增加高度。我几乎没有得到答案。这些不适合我的情况 案例: 我有高度默认为150的表格单元格。表单元格中添加的内容(Titile、Image、action)是表单元格中的视图。我们通过“UIImageView+AFNetworking.h”加载图像。我们有不同高度的图像Iphone 动态增加表格单元格的高度,iphone,ios,uitableview,Iphone,Ios,Uitableview,我搜索表格单元格的增加高度。我几乎没有得到答案。这些不适合我的情况 案例: 我有高度默认为150的表格单元格。表单元格中添加的内容(Titile、Image、action)是表单元格中的视图。我们通过“UIImageView+AFNetworking.h”加载图像。我们有不同高度的图像 在单元中加载图像后,如何增加/减少单元高度 在tableview中加载图像后,计算加载图像的高度。在高度中为rowatinexpath指定行高 试试这个代码 -(CGFloat)tableView:(UITabl
在单元中加载图像后,如何增加/减少单元高度 在tableview中加载图像后,计算加载图像的高度。在
高度中为rowatinexpath
指定行高
试试这个代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *Image= [imageAry objectAtIndex:indexPath.row];
NSLog(@"%f",Image.frame.size.height);
return size.height + 10;
}
只需使用如下委托方法设置单元格高度即可
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *Tempimage= [yourArray objectAtIndex:indexPath.row];
UIImageView *imgTemp = [[UIImageView alloc]initWithImage:Tempimage];
return imgTemp.frame.size.height;
}
为此,您必须重新加载特定单元格
// Build the index path
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:3 inSection:2];
// Add them in an index path array
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, nil];
// Launch reload for the two index path
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
并且必须从tableView:heightforrowatinexpath:
返回单元格高度,然后它将调用您的tableView:cellforrowatinexpath:
另外,我假设您正在进行延迟加载(在创建单元格后加载图像)我没有使用
UIImageView+AFNetworking.h
,但计算行高的想法是在某些位置加载后计算图像的高度
希望这个方法对你有帮助
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
在success
中加载图像后,计算图像高度image.size.height
并将其添加到表的数组中
NSMutableDictionary *record = [array objectAtIndex:index];
[record setObject:[NSString stringWithFormat:@"%f",image.size.height] forKey:@"imageheight"];
[array replaceObjectAtIndex:index withObject:record];
[tableView reloadData];
然后像这样设置行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *record = [array objectAtIndex:index];
if ( [record objectForkey:@"imageheight"] ) {
return [[record objectForkey:@"imageheight"] floatValue] + yourmargin);
// yourmargin may be 150 or what ever be
} else {
return 150;
}
}
下载图像后,写下以下行: [self.tblStoryCardView beginUpdates]; [self.tblStoryCardView endUpdates] 并采用行高法下载图像高度 我在github中上传了一个虚拟项目。您可以查看:
请附上表格截图。很难告诉您解决方案,因为“问题”的关键仍然没有找到:您何时获得当前单元格的图像(及其大小)?UIImage没有边框及其高度之类的属性,因此它会给出错误。谢谢您的回答。是否可以不计算图像高度?下载图像后,调整单元格的高度。根据您的回答,我们手中有所有的东西,然后我们计算高度。甚至我尝试使用“setImageWithURLRequest”下载图像,但图像无法下载。同时,在执行数据源函数“HeightForRowatineXpath”后,控件进入“success”块。这里没有图像高度。您必须
[tableView reloadData]
在success
方法中,直到成功不被称为高度为150,请在您的答案中张贴您的代码。链接可能会随着时间的推移而变得无效。这是我的github库链接,它工作正常