ios UITableView不同的单元格高度

时间:2014-01-07 20:35:18

标签: ios tableview

我正在开发一个应用程序,它要求我在TableView中显示图片和一些文本。这些图片可能具有不同的高度,因此我需要相应地改变单元格高度。所以我改写了这个方法:

 - (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath

如果我对单元格标识符有一个静态值,则单元格内图像的高度不能动态变化。

那么我是否需要为每个细胞设置不同的细胞标识符值?还有其他方法吗?

我不能使用除Tableview之外的其他视图,因为我需要根据用户交互动态显示一些单元格。

感谢。

5 个答案:

答案 0 :(得分:2)

您应该引用数据模型以获取图像大小并返回行高。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        UIImage *image = dataModel[indexPath.row];
        return image.size.height + 16.0; //16 is for padding
}

如果您对单元格进行子类化,则可以在-layoutsubviews中调整imageView框架(超级之后):

- (void)setupImageView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;
    self.contentImageView = imageView;

    [self.contentView addSubview:imageView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGRect bounds = self.contentView.bounds;
    float margin = 8.0;
    CGRect textViewFrame = CGRectZero;
    textViewFrame = CGRectMake(margin, roundf(margin * 2.0), bounds.size.width - (margin * 2.0), roundf(bounds.size.height - (margin * 4.0)));

    self.contentImageView.frame = textViewFrame;
}

答案 1 :(得分:1)

对于动态tableViewCell高度,请使用此。

-(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;

}

答案 2 :(得分:0)

仅图像的示例:

- (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.
}

有关更全面的示例,请查看我的答案:

Resize Custom cell with a UILabel on it based on content text from JSON file

答案 3 :(得分:0)

计算细胞高度

- (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);
}

有关详细信息,请参阅Link..

答案 4 :(得分:0)

您应该在单元格显示之前计算每个单元格的高度。因此,您可以编写一个方法来获取高度,然后使用您在模型中编写的方法。

相关问题