簡體   English   中英

ios TableView不同的單元格高度

[英]ios TableView different cell height

我正在開發一個應用程序,它要求我在TableView中顯示圖片和一些文本。 這些圖片可能具有不同的高度,因此我需要相應地改變單元格高度。 所以我重寫了這個方法:

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

如果我有一個單元格標識符的靜態值,那么單元格內圖像的高度不能動態變化。

那么我是否需要為每個細胞設置不同的細胞標識符值? 還有其他方法嗎?

我不能使用除Tableview之外的其他視圖,因為我需要根據用戶交互動態顯示一些單元格。

謝謝。

您應該引用數據模型以獲取圖像大小並返回行高。

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

如果您對單元格進行子類化,則可以在-layoutsubviews調整-layoutsubviews框架(超級之后):

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

對於動態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;

}

僅針對圖像的示例:

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

有關更全面的示例,請查看我的答案:

根據JSON文件中的內容文本,使用UILabel調整自定義單元格大小

計算細胞高度

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

有關更多參考,請參閱鏈接..

您應該在單元格顯示之前計算每個單元格的高度。因此,您可以編寫一個方法來獲取高度,然后使用您在模型中編寫的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM