zoukankan      html  css  js  c++  java
  • 新浪微博动态cell的计算总结

    1.自定义cell

    2.在cell中要把显示的控件全部添加上

    3.创建Frame模型,这个模型中包含数据模型(Status)@property (nonatomic, strong) PFStatus *status; // 模型;还要包含要显示控件的属性,例如:/**原创微博的整体*/@property (nonatomic, assign) CGRect originalViewF; 最主要的是要包含这个属性,/** cell的高度 */@property (nonatomic, assign) CGFloat cellHeight;

    4.在frame模型中实现 - (void)setStatus:(PFStatus *)status,这个setter方法。

    5.注意:昵称、时间、微博的来源、正文、配图等这些控件的宽度不能写死,必须得动态计算。动态计算这些控件的frame时,需要实现一个类目方法。

    - (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW

    {

        NSMutableDictionary *atts = [NSMutableDictionary dictionary];

        atts[NSFontAttributeName] = font;

        CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);

        return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:atts context:nil].size;

    }

     - (CGSize)sizeWithFont:(UIFont *)font

    {

        return [self sizeWithFont:font maxW:MAXFLOAT];

    }

    这两个方法就可以动态计算文字的frame。比如:要计算昵称的frame时,就应该这样来实现:

    CGFloat nameX = CGRectGetMaxX(self.iconViewF) + PFStatusCellBorderW;

    CGFloat nameY = iconY;

    CGSize nameSize = [user.name sizeWithFont:PFStatusCellNameFont];

    self.nameLabelF = (CGRect){{nameX,nameY},nameSize}; 这个直接写成的CGRect

    6.还有一个注意点就是计算配图的frame,配图有的显示一张或两张或九张图片,那么这个地方是怎么处理呢?首先自定义一个UIView来显示配图,模型中有一个显示配图的数组,根据这个数组来创建要显示的配图,

    - (void)setPhotosArr:(NSArray *)photosArr

    {

        _photosArr = photosArr;

        

        int photosCount = (int)photosArr.count;

        

        // 创建足够数量的图片控件

        //

        while (self.subviews.count < photosCount) {

            PFStatusPhotoView *photoView = [[PFStatusPhotoView alloc] init];

            [self addSubview:photoView];

        }

        

        // 遍历所有的图片控件,设置图片

        for (int i = 0; i < self.subviews.count; i++) {

            PFStatusPhotoView *photoView = self.subviews[i];

            if (i < photosCount) { // 显示

                photoView.photos = photosArr[i];

                photoView.hidden = NO;

            } else { // 隐藏

                photoView.hidden = YES;

            }

        }

    }

    + (CGSize)sizeWithCount:(int)count

    {

        NSInteger maxCols = count == 4 ? 2 : 3;

      // 列数

        NSInteger cols = (count >= maxCols) ? maxCols : count;

        CGFloat photoW = cols * 70 + (cols -1) * 10;

        

        // 行数

        NSInteger rows = (count + maxCols - 1) / maxCols;

        CGFloat phtotH = rows * 70 + (rows - 1) * 10;

        return CGSizeMake(photoW, phtotH);

    }

    - (void)layoutSubviews

    {

        [super layoutSubviews];

        

        // 设置图片的尺寸和位置

        NSInteger photosCount = self.photosArr.count;

        NSInteger maxCol = photosCount == 4 ? 2 : 3;

        for (NSInteger i = 0; i < photosCount; i++) {

            PFStatusPhotoView *photoView = self.subviews[i];

            NSInteger col = i % maxCol;

            photoView.x = col * (70 + 10);

            NSInteger row = i / maxCol;

            photoView.y = row * (70 + 10);

            photoView.width = 70;

            photoView.height = 70;

        }

    }

    PFStatusPhotoView 这个是自定义的配图imageView

    7.计算cell的高度

    self.cellHeight = CGRectGetMaxY(self.toolbarF) + PFStatueCellMargin;

    通过以上步骤就动态计算出cell的高度了

    1
  • 相关阅读:
    分享AWS网站
    centos7划分vlan
    在docker容器上如何实现代码的版本管理
    在docker私有仓库如何查看有哪些镜像?
    centos7下报错: import requests ImportError: No module named requests
    Unity2018.4.7导出Xcode工程报错解决方案
    1.OpenGL mac开发环境搭建记录
    unity 模板测试 详解
    游戏战争迷雾
    Unity 移动平台自己编写Shader丢失问题
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/5757363.html
Copyright © 2011-2022 走看看