iOS cell 高度计算

本文介绍了一种iOS中计算UITableViewCell高度的方法,并针对不同类型的帖子(如图片、语音和视频)调整布局。通过计算文本高度及图片等元素的尺寸,确保了自适应布局的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS cell 高度计算

  • heightForCell:

    • 每一个cell对应一个模型,模型里面有cell的高度,以及对图片frame的重新赋值;
      - (CGFloat)cellHeight
      {
      if (!_cellHeight) {
          // 文字的最大尺寸
          CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * XMGTopicCellMargin, MAXFLOAT);
          // 计算文字的高度
          CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
      
          // cell的高度
          // 文字部分的高度
          _cellHeight = XMGTopicCellTextY + textH + XMGTopicCellMargin;
      
          // 根据段子的类型来计算cell的高度
          if (self.type == XMGTopicTypePicture) { // 图片帖子
              if (self.width != 0 && self.height != 0) {
                  // 图片显示出来的宽度
                  CGFloat pictureW = maxSize.width;
                  // 显示显示出来的高度
                  CGFloat pictureH = pictureW * self.height / self.width;
                  if (pictureH >= XMGTopicCellPictureMaxH) { // 图片高度过长
                      pictureH = XMGTopicCellPictureBreakH;
                      self.bigPicture = YES; // 大图
                  }
      
                  // 计算图片控件的frame
                  CGFloat pictureX = XMGTopicCellMargin;
                  CGFloat pictureY = XMGTopicCellTextY + textH + XMGTopicCellMargin;
                  _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
      
                  _cellHeight += pictureH + XMGTopicCellMargin;
              }
          } else if (self.type == XMGTopicTypeVoice) { // 声音帖子
              CGFloat voiceX = XMGTopicCellMargin;
              CGFloat voiceY = XMGTopicCellTextY + textH + XMGTopicCellMargin;
              CGFloat voiceW = maxSize.width;
              CGFloat voiceH = voiceW * self.height / self.width;
              _voiceF = CGRectMake(voiceX, voiceY, voiceW, voiceH);
      
              _cellHeight += voiceH + XMGTopicCellMargin;
          } else if (self.type == XMGTopicTypeVideo) { // 视频帖子
              CGFloat videoX = XMGTopicCellMargin;
              CGFloat videoY = XMGTopicCellTextY + textH + XMGTopicCellMargin;
              CGFloat videoW = maxSize.width;
              CGFloat videoH = videoW * self.height / self.width;
              _videoF = CGRectMake(videoX, videoY, videoW, videoH);
      
              _cellHeight += videoH + XMGTopicCellMargin;
          }
      
          // 如果有最热评论
          if (self.top_cmt) {
              NSString *content = [NSString stringWithFormat:@"%@ : %@", self.top_cmt.user.username, self.top_cmt.content];
              CGFloat contentH = [content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size.height;
              _cellHeight += XMGTopicCellTopCmtTitleH + contentH + XMGTopicCellMargin;
          }
      
          // 底部工具条的高度
          _cellHeight += XMGTopicCellBottomBarH + XMGTopicCellMargin;
      }
      return _cellHeight;
      }
  • 返回cell的高度




  • 给子视图传数据

    
    - (void)setTopic:(XMGTopic *)topic
    {
    _topic = topic;
    
    // 新浪加V
    self.sinaVView.hidden = !topic.isSina_v;
    
    // 设置头像
    [self.profileImageView setHeader:topic.profile_image];
    
    // 设置名字
    self.nameLabel.text = topic.name;
    
    // 设置帖子的创建时间
    self.createTimeLabel.text = topic.create_time;
    
    // 设置按钮文字
    [self setupButtonTitle:self.dingButton count:topic.ding placeholder:@"顶"];
    [self setupButtonTitle:self.caiButton count:topic.cai placeholder:@"踩"];
    [self setupButtonTitle:self.shareButton count:topic.repost placeholder:@"分享"];
    [self setupButtonTitle:self.commentButton count:topic.comment placeholder:@"评论"];
    
    // 设置帖子的文字内容
    self.text_label.text = topic.text;
    
    // 根据模型类型(帖子类型)添加对应的内容到cell的中间
    if (topic.type == XMGTopicTypePicture) { // 图片帖子
        self.pictureView.hidden = NO;
        self.pictureView.topic = topic;
        self.pictureView.frame = topic.pictureF;
    
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    } else if (topic.type == XMGTopicTypeVoice) { // 声音帖子
        self.voiceView.hidden = NO;
        self.voiceView.topic = topic;
        self.voiceView.frame = topic.voiceF;
    
        self.pictureView.hidden = YES;
        self.videoView.hidden = YES;
    } else if (topic.type == XMGTopicTypeVideo) { // 视频帖子
        self.videoView.hidden = NO;
        self.videoView.topic = topic;
        self.videoView.frame = topic.videoF;
    
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    } else { // 段子帖子
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    }
    // 处理最热评论
    if (topic.top_cmt) {
        self.topCmtView.hidden = NO;
        self.topCmtContentLabel.text = [NSString stringWithFormat:@"%@ : %@", topic.top_cmt.user.username, topic.top_cmt.content];
    } else {
        self.topCmtView.hidden = YES;
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值