ios TableView不同的单元格高度
我正在开发一个应用程序,它要求我在TableView中显示图片和一些文本。这些图片可以有不同的高度,因此我需要相应地改变单元格高度。因此,我已重写此方法:ios TableView不同的单元格高度,ios,tableview,Ios,Tableview,我正在开发一个应用程序,它要求我在TableView中显示图片和一些文本。这些图片可以有不同的高度,因此我需要相应地改变单元格高度。因此,我已重写此方法: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 如果单元格标识符只有一个静态值,那么单元格内图像的高度就不能动态变化 那么,我需要为每个单元格设置不同的单元格标识符值吗?还有别的办法吗 我不能使用T
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
如果单元格标识符只有一个静态值,那么单元格内图像的高度就不能动态变化
那么,我需要为每个单元格设置不同的单元格标识符值吗?还有别的办法吗
我不能使用Tableview以外的其他视图,因为我需要根据用户交互动态显示其间的一些单元格
谢谢。仅图像示例:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//get reference to a image
UIImage *image = [arrImages objectAtIndex:indexPath.row];
//get the height of your imageview on the cell
CGFloat imageViewHeight = image.size.height/2;//RetinaDisplay
//return the height of the imageview (plus some padding) for your cell height
return imageViewHeight; //add here also all other constant height's of objects that you have on your cell.
}
有关更全面的示例,请参见我的回答:
您应该参考数据模型以获取图像大小并返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = dataModel[indexPath.row];
return image.size.height + 16.0; //16 is for padding
}
如果对单元格进行子类化,则可以在-layoutsubviews
中调整imageView框架(在super之后):
计算单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
有关更多参考信息,请参见。在单元格显示之前,您应该计算每个单元格的高度。因此,您可以编写一个方法来获取高度,然后使用您在模型中编写的方法。对于动态表视图单元格高度,请使用此方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelHeight = [self heigtForCellwithString:yourLabel.text withFont:yourLabel.font];
return labelHeight.height; // the return height + your other view height
}
-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont)font{
CGSize constraint = CGSizeMake(300,9999); // Replace 300 with your label width
NSDictionary *attributes = @{NSFontAttributeName: font};
CGRect rect = [stringValue boundingRectWithSize:constraint
options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil];
return rect.size;
}
–sizeWithFont:…
方法从iOS7开始使用。:/您应该在此处使用例如–sizeWithAttributes:
方法。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelHeight = [self heigtForCellwithString:yourLabel.text withFont:yourLabel.font];
return labelHeight.height; // the return height + your other view height
}
-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont)font{
CGSize constraint = CGSizeMake(300,9999); // Replace 300 with your label width
NSDictionary *attributes = @{NSFontAttributeName: font};
CGRect rect = [stringValue boundingRectWithSize:constraint
options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil];
return rect.size;
}