美文网首页
iOS UITableView结合Masonry布局,微仿朋友圈

iOS UITableView结合Masonry布局,微仿朋友圈

作者: 邓布利多教授 | 来源:发表于2019-08-06 11:41 被阅读0次

    前段时间发了一篇关于Cell自适应高度的文章,文章结尾承诺会更新一篇带有类似朋友圈的评论框的布局的文章。其实也不算是一种承诺,我自己更认为是给自己留的一个homework。哈哈哈,好了,废话到此为止。

    结构,项目结构就是这么简单的一个结构。

    Tools里有两个小工具:
    1、HYBMasonryAutoCellHeight,来自标哥的这个工具
    2、LYMasonryAutoFooterViewHeight,是我仿照标哥这个工具照抄的,不想照抄的朋友看这里
    Model就不用说了,解析数据的
    View也是,自定义的一些view

    结构图.png

    看下效果图吧

    效果图.gif
    • 其实我个效果就是把我前面发过的几个文章结合了,整理整理放到一起,就是这样的效果。接下来简单说一下吧。

    1、Model

    Model其实很简单,就是把数据源的字段生成属性,没什么可说的,代码如下:

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface FriendsCircleModel : NSObject
    
    @property (nonatomic, strong) NSString *userPic;
    @property (nonatomic, strong) NSString *userName;
    @property (nonatomic, strong) NSString *publicTime;
    @property (nonatomic, strong) NSString *content;
    @property (nonatomic, assign) NSInteger imgCount;
    @property (nonatomic, strong) NSArray *commentCount;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    2、View

    直接上代码

    • FriendCircleCell.h
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @class FriendsCircleModel;
    
    @interface FriendCircleCell : UITableViewCell
    
    -(void)setModel:(FriendsCircleModel *)model;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    • FriendCircleCell.m
    #import "FriendCircleCell.h"
    #import "FriendsCircleModel.h"
    #import "FriendCircleImgView.h"
    
    @interface FriendCircleCell ()
    
    @property (nonatomic, strong) UIView *vLine;
    @property (nonatomic, strong) UIImageView *ivUser;
    @property (nonatomic, strong) UILabel *lName;
    @property (nonatomic, strong) UILabel *lTime;
    @property (nonatomic, strong) UILabel *lContent;
    @property (nonatomic, strong) FriendCircleImgView *fcIView;
    
    @end
    
    @implementation FriendCircleCell
    
    -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.contentView.backgroundColor = [UIColor whiteColor];
            self.selectionStyle = UITableViewCellSelectionStyleNone;
            [self createUI];//创建UI
        }
        return self;
        
    }
    
    #pragma mark - 创建UI
    -(void)createUI{
        
        //上划线
        _vLine = [UIView new];
        _vLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:_vLine];
        
        //头像
        _ivUser = [UIImageView new];
        _ivUser.clipsToBounds = YES;
        _ivUser.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_ivUser];
        
        //用户名
        _lName = [UILabel new];
        _lName.textColor = [UIColor grayColor];
        _lName.textAlignment = NSTextAlignmentLeft;
        _lName.font = [UIFont systemFontOfSize:14];
        [self addSubview:_lName];
        
        //时间
        _lTime = [UILabel new];
        _lTime.textColor = [UIColor lightGrayColor];
        _lTime.textAlignment = NSTextAlignmentLeft;
        _lTime.font = [UIFont systemFontOfSize:12];
        [self addSubview:_lTime];
        
        //内容
        _lContent = [UILabel new];
        _lContent.textColor = [UIColor blackColor];
        _lContent.textAlignment = NSTextAlignmentLeft;
        _lContent.font = [UIFont systemFontOfSize:14];
        _lContent.numberOfLines = 0;
        [self addSubview:_lContent];
        //必须设置UILabel的最大宽度
        _lContent.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 90;
        
        //图片
        _fcIView = [[FriendCircleImgView alloc]initWithMaxWidth:[UIScreen mainScreen].bounds.size.width - 90];
        [self addSubview:_fcIView];
        
        //下面两句必须要写
        self.hyb_lastViewInCell = _fcIView;//最后一个视图
        
    }
    
    #pragma mark - 创建布局
    -(void)createLayout{
        
        //上划线
        [_vLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.height.mas_equalTo(1);
        }];
        
        //头像
        [_ivUser mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(20);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        
        //用户名
        [_lName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.ivUser.mas_right).offset(10);
            make.top.mas_equalTo(self.ivUser.mas_top);
            make.height.mas_equalTo(20);
            make.right.mas_equalTo(self.mas_right).offset(-20);
        }];
        
        //时间
        [_lTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.lName.mas_left);
            make.top.mas_equalTo(self.lName.mas_bottom).offset(5);
            make.height.mas_equalTo(10);
        }];
        
        //内容
        [_lContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.lName.mas_left);
            make.top.mas_equalTo(self.lTime.mas_bottom).offset(10);
            make.right.mas_equalTo(self.mas_right).offset(-20);
        }];
        
        //图片
        [_fcIView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.lContent.mas_left);
            make.top.mas_equalTo(self.lContent.mas_bottom).offset(20);
            make.right.mas_equalTo(self.lContent.mas_right);
        }];
        
    }
    
    -(void)layoutSubviews{
        [super layoutSubviews];
        [self createLayout];//创建布局
    }
    
    #pragma mark - 添加数据源
    -(void)setModel:(FriendsCircleModel *)model{
        
        _ivUser.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _lName.text = model.userName;
        _lTime.text = model.publicTime;
        _lContent.text = model.content;
        _fcIView.buttonCount = model.imgCount;
        
        //如果没有图片,改变图片视图距离底部边距
        if (model.imgCount == 0) {
            self.hyb_bottomOffsetToCell = 0;//最后一个视图距离底边距,默认为0
        }else{
            self.hyb_bottomOffsetToCell = 10;//最后一个视图距离底边距,默认为0
        }
        
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    }
    
    @end
    

    FriendCircleImgView这个小工具之前发过文章,文章地址在这里

    • FriendCircleImgView.h
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface FriendCircleImgView : UIView
    
    ///初始化的时候传入最大宽度,方便布局使用
    -(instancetype)initWithMaxWidth:(CGFloat)width;
    
    ///本次需要添加宫格的个数
    @property (nonatomic, assign) NSInteger buttonCount;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    • FriendCircleImgView.m
    #import "FriendCircleImgView.h"
    
    typedef enum : NSUInteger {
        FCButtonLayoutStyleOne,//一张图片
        FCButtonLayoutStyleTwo,//两张图片
        FCButtonLayoutStyleFour,//四张图片
        FCButtonLayoutStyleOther,//其他
    } FCButtonLayoutStyle;
    
    @interface FriendCircleImgView ()
    
    ///装多宫格的数组,复用视图
    @property (nonatomic, strong) NSMutableArray <UIButton*>* buttons;
    ///容器视图 来自适应九宫格高度
    @property (nonatomic, strong) UIView * containerView;
    ///容器视图的底部约束
    @property (nonatomic, weak) MASConstraint * containerViewConstraintbottom;
    ///button布局类型
    @property (nonatomic) FCButtonLayoutStyle layoutStyle;
    ///最大宽度
    @property (nonatomic) CGFloat maxWidth;
    
    @end
    
    @implementation FriendCircleImgView
    
    -(void)setButtonCount:(NSInteger)buttonCount{
        
        //有图片的情况
        if (buttonCount != 0) {
            
            if (buttonCount == 1) {
                self.layoutStyle = FCButtonLayoutStyleOne;
            }else if (buttonCount == 2){
                self.layoutStyle = FCButtonLayoutStyleTwo;
            }else if (buttonCount == 4){
                self.layoutStyle = FCButtonLayoutStyleFour;
            }else{
                self.layoutStyle = FCButtonLayoutStyleOther;
            }
            
            //每次重新创建宫格的个数 从容器视图中移除
            [self.containerView.subviews enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (!obj.hidden) {
                    [obj removeFromSuperview];
                    obj.hidden = true;
                }
            }];
            
            //循环从初始化list中取出在添加到容器视图当中
            for (int i = 0; i< buttonCount; i++) {
                UIButton *btn = self.buttons[i];
                [self.containerView addSubview:btn];
                btn.hidden = false;
            }
            
            //间距x,y
            CGFloat marginX = 10.0;
            CGFloat marginY = marginX;
            
            
            //一张图片的情况
            if (self.layoutStyle == FCButtonLayoutStyleOne) {
                
                //button宽度
                CGFloat btnWidth = self.maxWidth - marginX * 2;
                
                UIButton *subv = self.containerView.subviews[0];
                subv.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
                
                [subv mas_updateConstraints:^(MASConstraintMaker *make){
                    make.left.mas_equalTo(marginX);
                    make.top.mas_equalTo(marginY);
                    make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
                }];
                
            }
            
            //两张图片的情况
            if (self.layoutStyle == FCButtonLayoutStyleTwo) {
                
                //button宽度
                CGFloat btnWidth = (self.maxWidth - marginX * 3) / 2;
                
                for (int i = 0; i < self.containerView.subviews.count ; i++) {
                    
                    UIButton *subv = self.containerView.subviews[i];
                    subv.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
                    
                    [subv mas_updateConstraints:^(MASConstraintMaker *make){
                        make.left.mas_equalTo(marginX + i % 2 * (btnWidth + marginX));
                        make.top.mas_equalTo(marginY);
                        make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
                    }];
                    
                }
                
            }
            
            //四张图片的情况
            if (self.layoutStyle == FCButtonLayoutStyleFour) {
                
                //button宽度
                CGFloat btnWidth = (self.maxWidth - marginX * 3) / 2;
                
                for (int i = 0; i < self.containerView.subviews.count ; i++) {
                    
                    UIButton *subv = self.containerView.subviews[i];
                    subv.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
                    
                    [subv mas_updateConstraints:^(MASConstraintMaker *make){
                        make.left.mas_equalTo(marginX + i % 2 * (btnWidth + marginX));
                        make.top.mas_equalTo(marginY + i / 2 * (btnWidth + marginY));
                        make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
                    }];
                    
                }
                
            }
            
            //其他情况
            if (self.layoutStyle == FCButtonLayoutStyleOther) {
                
                //button宽度
                CGFloat btnWidth = (self.maxWidth - marginX * 4) / 3;
                
                for (int i = 0; i < self.containerView.subviews.count ; i++) {
                    
                    UIButton *subv = self.containerView.subviews[i];
                    subv.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
                    
                    [subv mas_updateConstraints:^(MASConstraintMaker *make){
                        make.left.mas_equalTo(marginX + i % 3 * (btnWidth + marginX));
                        make.top.mas_equalTo(marginY + i / 3 * (btnWidth + marginY));
                        make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
                    }];
                    
                }
                
            }
            
            //卸载上一次容器视图的底部约束
            if (self.containerViewConstraintbottom) {
                [self.containerViewConstraintbottom uninstall];
            }
            //重新生成容器视图的底部约束 参考最后一个宫格的底部
            [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.containerViewConstraintbottom = make.bottom.equalTo(self.containerView.subviews.lastObject).offset(marginX);
            }];
            [self mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.containerView.mas_bottom);
            }];
            
        }
        
        //无图片的情况
        else{
            
            //每次重新创建宫格的个数 从容器视图中移除
            [self.containerView.subviews enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (!obj.hidden) {
                    [obj removeFromSuperview];
                    obj.hidden = true;
                }
            }];
            //卸载上一次容器视图的底部约束
            if (self.containerViewConstraintbottom) {
                [self.containerViewConstraintbottom uninstall];
            }
            //重新生成容器视图的底部约束 参考最后一个宫格的底部
            [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.containerViewConstraintbottom = make.bottom.equalTo(self.containerView.mas_top).offset(0);
            }];
            [self mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.containerView.mas_bottom);
            }];
            
        }
        
    }
    
    -(instancetype)initWithMaxWidth:(CGFloat)width{
        
        self = [super init];
        if (self) {
            
            self.maxWidth = width;
            self.backgroundColor = [UIColor groupTableViewBackgroundColor];
            for (int i = 0; i < 9; i++) {
                UIButton * button = [UIButton new];
                button.hidden = true;
                [self.buttons addObject:button];
            }
            [self addSubview:self.containerView];
            [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.equalTo(self);
            }];
            
        }
        return self;
        
    }
    
    /// MARK: ---- 懒加载
    -(NSMutableArray *)buttons {
        if (!_buttons) {
            _buttons = [NSMutableArray array];
        }
        return _buttons;
    }
    -(UIView *)containerView {
        if (!_containerView) {
            _containerView = [UIView new];
        }
        return _containerView;
    }
    
    @end
    
    • FriendCircleFooterView.h
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @class FriendsCircleModel;
    
    @interface FriendCircleFooterView : UITableViewHeaderFooterView
    
    -(void)setModel:(FriendsCircleModel *)model;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    • FriendCircleFooterView.m
    #import "FriendCircleFooterView.h"
    #import "FriendsCircleModel.h"
    
    @interface FriendCircleFooterView ()
    
    @property (nonatomic, strong) UIView *ivbg;
    
    @end
    
    @implementation FriendCircleFooterView
    
    -(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
        
        self = [super initWithReuseIdentifier:reuseIdentifier];
        if (self) {
            self.contentView.backgroundColor = [UIColor whiteColor];
            [self createUI];//创建UI
        }
        return self;
        
    }
    
    #pragma mark - 创建UI
    -(void)createUI{
        
        _ivbg = [UIView new];
        _ivbg.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.contentView addSubview:_ivbg];
        
    }
    
    -(void)setModel:(FriendsCircleModel *)model{
    
        //先移除self.vbg的所有子视图
        [self.ivbg.subviews enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
        
        //再重新创建新的子视图
        [self createSubView:model];
        
    }
    
    #pragma mark - 评论View
    -(void)createSubView:(FriendsCircleModel *)model{
        
        __weak typeof(self) weakSelf = self;
        
        //评论数不为0的时候
        if (model.commentCount.count != 0) {
            
            UILabel *lastLabel = nil;
            for (int i = 0; i < model.commentCount.count; i ++) {
                
                UILabel *lComment = [UILabel new];
                lComment.text = model.commentCount[i];
                lComment.textColor = [UIColor blackColor];
                lComment.textAlignment = NSTextAlignmentLeft;
                lComment.font = [UIFont systemFontOfSize:12];
                lComment.numberOfLines = 0;
                [weakSelf.ivbg addSubview:lComment];
                //必须设置UILabel视图的最大宽度
                lComment.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 110;
                
                [lComment mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    if (lastLabel) {
                        make.top.mas_equalTo(lastLabel.mas_bottom).offset(5);
                    } else {
                        make.top.mas_equalTo(weakSelf.ivbg.mas_top).offset(20);
                    }
                    
                    make.left.mas_equalTo(weakSelf.ivbg.mas_left).offset(10);
                    make.right.mas_equalTo(weakSelf.ivbg.mas_right).offset(-10);
                    
                }];
                
                lastLabel = lComment;
                
            }
            
            [_ivbg mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(70);
                make.top.mas_equalTo(weakSelf.contentView.mas_top);
                make.right.mas_equalTo(weakSelf.contentView.mas_right).offset(-20);
                make.bottom.mas_equalTo((weakSelf.ivbg.subviews.lastObject).mas_bottom).offset(10);
                
            }];
            
        }
        
        //评论数为0的时候
        else{
            
            [_ivbg mas_updateConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(weakSelf.contentView);
            }];
            
        }
        
        //下面两句必须要写
        self.ly_lastViewInView = _ivbg;//最后一个视图
        self.ly_bottomOffsetToView = 0;//最后一个视图距离底边距,默认为0
        
    }
    
    @end
    

    3、Controller

    控制器里面就是创建了TableView、数据源和自适应高度工具的用法。
    值得说一下的是UITableView的UITableViewHeaderFooterView的复用的问题,这点肯定被很多人都忽视了,都知道cell可以复用,其实HeaderFooterView也可以这么用,不过这些都是看需求和自己处理逻辑的方式来定的。用法和cell的复用一毛一样。

    #import "FriendsCircleController.h"
    #import "FriendCircleCell.h"
    #import "FriendsCircleModel.h"
    #import "FriendCircleFooterView.h"
    
    @interface FriendsCircleController ()<UITableViewDelegate,UITableViewDataSource>
    
    @property (nonatomic, strong) UITableView *tvList;
    @property (nonatomic, strong) NSMutableArray *dataSource;
    
    @end
    
    @implementation FriendsCircleController
    
    -(void)viewDidLayoutSubviews{
        
        [super viewDidLayoutSubviews];
        
        [_tvList mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.view);
        }];
        
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        [self.view addSubview:self.tableView];
    
        for (NSDictionary *dict in [self arrayDataSource]) {
    
            FriendsCircleModel *model = [FriendsCircleModel new];
            model.userPic = dict[@"user_pic"];
            model.userName = dict[@"user_name"];
            model.publicTime = dict[@"public_time"];
            model.content = dict[@"content"];
            model.imgCount = [dict[@"img_count"] integerValue];
            model.commentCount = dict[@"comment_count"];
            [self.dataSource addObject:model];
    
        }
        [_tvList reloadData];
        
    }
    
    #pragma mark - TableView
    -(UITableView *)tableView{
        
        if (!_tvList) {
            _tvList = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
            _tvList.delegate = self;
            _tvList.dataSource = self;
            _tvList.separatorStyle = UITableViewCellSeparatorStyleNone;
            _tvList.backgroundColor = [UIColor whiteColor];
        }
        return _tvList;
        
    }
    
    #pragma mark - TableViewDataSource
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.dataSource.count;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 1;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        static NSString *ident = @"Cell";
        FriendCircleCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
        if (!cell) {
            cell = [[FriendCircleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
        }
        
        FriendsCircleModel *model = self.dataSource[indexPath.section];
        [cell setModel:model];
        
        return cell;
        
    }
    
    #pragma mark - TableViewDelegate
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        CGFloat height = [FriendCircleCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
            
            FriendCircleCell *regCell = (FriendCircleCell *)sourceCell;
            // 配置数据
            FriendsCircleModel *model = self.dataSource[indexPath.section];
            [regCell setModel:model];
            
        } cache:^NSDictionary *{
            
            return @{kHYBCacheUniqueKey: [NSString stringWithFormat:@"%ld",indexPath.section],
                     kHYBCacheStateKey : @"cell",
                     // 如果设置为YES,若有缓存,则更新缓存,否则直接计算并缓存s
                     // 主要是对社交这种有动态评论等不同状态,高度也会不同的情况的处理
                     kHYBRecalculateForStateKey : @(NO) // 标识要重新更新
                     };
            
        }];
        return height;
        
    }
    
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        return [UIView new];
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 0.0001;
    }
    
    -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
        
        static NSString *identF = @"FView";
        FriendCircleFooterView *vFooter = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identF];
        if (!vFooter) {
            vFooter = [[FriendCircleFooterView alloc]initWithReuseIdentifier:identF];
        }
        
        FriendsCircleModel *model = self.dataSource[section];
        [vFooter setModel:model];
        
        return vFooter;
        
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        
        CGFloat height = [FriendCircleFooterView ly_heightForTableView:tableView config:^(UITableViewHeaderFooterView *sourceView) {
    
            FriendCircleFooterView *regCell = (FriendCircleFooterView *)sourceView;
            // 配置数据
            FriendsCircleModel *model = self.dataSource[section];
            [regCell setModel:model];
    
        } cache:^NSDictionary *{
    
            return @{kLYCacheUniqueKey: [NSString stringWithFormat:@"%ld",section],
                     kLYCacheStateKey : @"headerView",
                     // 如果设置为YES,若有缓存,则更新缓存,否则直接计算并缓存s
                     // 主要是对社交这种有动态评论等不同状态,高度也会不同的情况的处理
                     kLYRecalculateForStateKey : @(NO) // 标识要重新更新
                     };
    
        }];
        return height;
        
    }
    
    #pragma mark - 数据源
    -(NSArray *)arrayDataSource{
        
        return @[@{@"user_pic":@"",
                   @"user_name":[NSString stringWithFormat:@"iOS攻城狮%u",arc4random() % 1000],
                   @"public_time":[NSString stringWithFormat:@"%u-%u-%u",arc4random() % 1000,arc4random() % 13,arc4random() % 30],
                   @"content":@"今天阳光明媚,今夜多云转晴",
                   @"img_count":@(arc4random() % 10),
                   @"comment_count":@[@"啊啊啊啊啊",
                                      @"不不不不不不不不"]},
                 @{@"user_pic":@"",
                   @"user_name":[NSString stringWithFormat:@"iOS攻城狮%u",arc4random() % 1000],
                   @"public_time":[NSString stringWithFormat:@"%u-%u-%u",arc4random() % 1000,arc4random() % 13,arc4random() % 30],
                   @"content":@"十年生死两茫茫,不思量,自难忘。千里孤坟,无处话凄凉。纵使相逢应不识,尘满面,鬓如霜。",
                   @"img_count":@(arc4random() % 10),
                   @"comment_count":@[@"sas挨打的"]},
                 @{@"user_pic":@"",
                   @"user_name":[NSString stringWithFormat:@"iOS攻城狮%u",arc4random() % 1000],
                   @"public_time":[NSString stringWithFormat:@"%u-%u-%u",arc4random() % 1000,arc4random() % 13,arc4random() % 30],
                   @"content":@"道人自嫌三世将,弃家十年今始壮。玉骨犹含富贵余,漆瞳已照人天上。去年相见古长干,众中矫矫如翔鸾。今年过我江西寺,病瘦已作霜松寒。朱颜不办供岁月,风中蒿火汤中雪。如问君家面黄翁,乞得摩尼照生灭。莫学王郎与支遁,臂鹰走马怜神骏。还君图画君自收,不如木人骑土牛。",
                   @"img_count":@(arc4random() % 10),
                   @"comment_count":@[@"坦尾我而想方设法的",
                                      @"奥术大师所大所多多挨打的阿斯达阿斯达多所收到",
                                      @"asparagus片速度快啊啥事大"]},
                 @{@"user_pic":@"",
                   @"user_name":[NSString stringWithFormat:@"iOS攻城狮%u",arc4random() % 1000],
                   @"public_time":[NSString stringWithFormat:@"%u-%u-%u",arc4random() % 1000,arc4random() % 13,arc4random() % 30],
                   @"content":@"白头萧散满霜风,小阁藤床寄病容。报道先生春睡美,道人轻打五更钟。",
                   @"img_count":@(arc4random() % 10),
                   @"comment_count":@[]}];
        
    }
    
    -(NSMutableArray *)dataSource{
        
        if (!_dataSource) {
            _dataSource = [NSMutableArray new];
        }
        return _dataSource;
        
    }
    
    @end
    
    • 全部代码都在这里,粘贴直接看效果。
      实现这种效果的方式肯定不止这一种,还有更优秀更完美的方式,欢迎评论一起玩耍。

    4、全剧终

    2019年11月28日——补充

    重点的一段:关于UILabel换行的显示,在基于auto layout布局里,必须每个可换行的UILabel都要设置preferredMaxLayoutWidth这个属性,否则会出现凌乱的效果。关于上面文章提到的内容,可能会有朋友没注意到这个属性的设置,在这里重点说明一下。

    相关文章

      网友评论

          本文标题:iOS UITableView结合Masonry布局,微仿朋友圈

          本文链接:https://www.haomeiwen.com/subject/hhcidctx.html