【问题标题】:ios TableView different cell heightios TableView不同的单元格高度
【发布时间】:2014-01-07 20:35:17
【问题描述】:

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

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

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

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

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

谢谢。

【问题讨论】:

    标签: ios tableview


    【解决方案1】:

    您应该参考您的数据模型来获取图像大小并返回行高。

    -(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之后):

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

    【讨论】:

      【解决方案2】:

      对于动态 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;
      
      }
      

      【讨论】:

        【解决方案3】:

        仅图像示例:

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

        【讨论】:

          【解决方案4】:

          计算像元高度

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

          【讨论】:

          • 自 iOS7 以来,–sizeWithFont:... 方法一直是 deprecated。 :/你应该在这里使用例如–sizeWithAttributes: 方法。
          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-01-28
            • 1970-01-01
            • 2012-10-28
            • 2017-11-14
            • 1970-01-01
            • 2017-07-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多