iOS cell 高度计算

    xiaoxiao2021-03-25  100

    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; } }
    转载请注明原文地址: https://ju.6miu.com/read-5484.html

    最新回复(0)