iOS收藏iOS学习键盘处理

iOS - 评论功能实现

2017-10-30  本文已影响124人  Mn_Su
473EC3D1187D837EFC648B3A0FED6903.png

前言:偷个懒,这篇文章写起来有些绕,还要修改做好的数据之类的,所以大概只写个思路,代码不用看的太过相信!

零、核心代码和思路

    1. 在控制器创建 commentView,然后赋值Model,在commentView.m中 setModel重新初始化,并赋值使用;

    2.评论功能分为 评论tableview 和 回复tableview;    

    3.在重新初始化 setModel set方法中,获取评论条数和model,根据评论内容计算出文本高度,然后重设评论tableview高度;

    4.在评论tableview 的 cellForRowAtIndexPath 方法里,获取回复内容的条数和model,然后根据回复内容计算出文本高度,然后用 cell的方法赋值到回复tableview中,刷新列表,代码:

        1)MSUVideoDetailCommentView中代码
            // 计算回复table 高度
            CGFloat tableViewHeight = 0.0;
            if (arrModel.reply.count > 0) {
                for (NSInteger i = 0; i < arrModel xss=removed> 0) {
                        str = [NSString stringWithFormat:@"%@ 回复 %@:%@",commentModel.from_user,commentModel.to_user,commentModel.content];;
                    }else {
                        str = [NSString stringWithFormat:@"%@:%@",commentModel.from_user,commentModel.content];
                    }
                    CGRect comnmentRect = [MSUStringTools danamicGetHeightFromText:str WithWidth:SelfWidth-28-36-5-20 font:10];
                    tableViewHeight = tableViewHeight + comnmentRect.size.height + 5;
                }
            } else{
                tableViewHeight = -15;
            }
            
            // 加上头部视图和底部试图
            [cell configCellWithModel:arrModel tableHeight:tableViewHeight+15];
        
            cell.lineView.frame = CGRectMake(55, CGRectGetMaxY(cell.commentLab.frame)+5+tableViewHeight+15+10-1, SelfWidth-55-14, 1);

        2) MSUVideoCommentTableCell 中代码

            - (void)configCellWithModel:(MSUArrModel *)model tableHeight:(NSInteger)height{
                self.arrModel = model;
            
                // 重设 回复table高度
                [_commentTableView remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(_commentLab.bottom).offset(5);
                    make.left.equalTo(_commentLab.left).offset(0);
                    make.width.equalTo(CommentTableWidth);
                    make.height.equalTo(height);
                }];
                
                [self.commentTableView reloadData];
            }
    
        5. 回复tableview 中 回复内容颜色是富文本设置,代码:

                    if (commentModel.to_user.length > 0) {
                        // 富文本设置
                        NSString *str = [NSString stringWithFormat:@"%@ 回复 %@:%@",commentModel.from_user,commentModel.to_user,commentModel.content];;
                        NSMutableAttributedString *att = [MSUStringTools makeKeyWordAttributedWithSubText:commentModel.to_user inOrigiText:str font:10 color:HEXCOLOR(0xf49418)];
                        cell.commentLab.attributedText = [MSUStringTools makeAttrubuteKeyWordAttributedWithSubText:commentModel.from_user inOrigiText:att font:10 color:HEXCOLOR(0xf49418)];
                    }else {
                        // 富文本设置
                        NSString *str = [NSString stringWithFormat:@"%@:%@",commentModel.from_user,commentModel.content];
                        cell.commentLab.attributedText = [MSUStringTools makeKeyWordAttributedWithSubText:commentModel.from_user inOrigiText:str font:10 color:HEXCOLOR(0xf49418)];
                    }

            6.评论view的整体高度回调,在评论tableview 中的 setModel方法中,遍历 评论条数和model 以及 回复条数和model,计算出内容高度, 然后回调给控制器 中的scrollview 的contentSize,代码:

                MSUVideoDetailCommentView中:
                
                - (void)setDataModel:(MSUDataDetailModel *)dataModel{
                    _dataModel = dataModel;
                    
                    if (_dataModel.comments.count > 0) {
                        self.titLab.text = [NSString stringWithFormat:@"评论(%ld)",_dataModel.comments.count];
                        self.comeLab.hidden = YES;
                        self.tableView.hidden = NO;
                    } else{
                        self.titLab.text = @"评论";
                        self.tableView.hidden = YES;
                        self.comeLab.hidden = NO;
                    }
                    
                    CGFloat height = 0.0;
                    for (NSInteger i = 0; i < _dataModel.comments.count; i++) {
                        MSUArrModel *arrModel = _dataModel.comments[i];
                        // 计算评论内容高度
                        CGRect rect = [MSUStringTools danamicGetHeightFromText:arrModel.content WithWidth:SelfWidth-65-14 font:11];
                        
                        // 计算回复table高度
                        CGFloat tableViewHeight = 0.0;
                        for (NSInteger i = 0; i < arrModel.reply.count; i++) {
                            MSUCommentModel *commentModel = arrModel.reply[i];
                            NSString *str;
                            if (commentModel.to_user.length > 0) {
                                str = [NSString stringWithFormat:@"%@ 回复 %@:%@",commentModel.from_user,commentModel.to_user,commentModel.content];;
                            }else {
                                str = [NSString stringWithFormat:@"%@:%@",commentModel.from_user,commentModel.content];
                            }
                            CGRect comnmentRect = [MSUStringTools danamicGetHeightFromText:str WithWidth:SelfWidth-28-36-5-20 font:10];
                            tableViewHeight = tableViewHeight + comnmentRect.size.height + 5;
                        }
                        
                        // 计算评论table高度
                       height = height + 28 + rect.size.height + 5 + tableViewHeight + 15 + 10;
                    }
                    
                    // 重设评论table高度
                    [_tableView remakeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(self.top).offset(35);
                        make.left.equalTo(self.left).offset(0);
                        make.width.equalTo(SelfWidth);
                        make.height.equalTo(height);
                    }];
                    
                    [self.tableView reloadData];
                    
                    if (self.tableHeightBlock) {
                        self.tableHeightBlock(height);
                    }
                }

            控制器中 : 
    
                __weak typeof(self) weakSelf = self;
                _comment.tableHeightBlock = ^(NSInteger height) {
                    // 更改滚动视图的滚动面积
                    weakSelf.bgScrollView.contentSize = CGSizeMake(0, 10+self.videoHeight+5+130+height+45);
                    // 更改评论父视图的frame
                    CGRect frame = weakSelf.comment.frame;
                    frame.size.height = 10+self.videoHeight+5+130+height;
                    weakSelf.comment.frame = frame;
                };

一、模型 MSUVideoDetailModel (因为做的是视频、商品,然后下面有评论,所以model内容较多,MSUArrModel和MSUCommentModel是评论的数据结构,其他的可以忽视)

    1.MSUVideoDetailModel.h中的代码

        #import <Foundation>

        @class MSUDataDetailModel;
        @class MSUDetailVideoModel;
        @class MSUDetailGoodsModel;
        @class MSUCommentModel;
        @class MSUArrModel;
        @class MSUSignDetailModel;
        
        
        @interface MSUVideoDetailModel : NSObject
        
        /// 成功码
        @property (nonatomic , assign) NSInteger code;
        
        /// data
        @property (nonatomic , strong) NSArray<MSUDataDetailModel> *data;
        
        /// msg
        @property (nonatomic , copy) NSString *msg;
        
        @end
        
        
        @interface MSUDataDetailModel : NSObject
        
        /// 商品
        @property (nonatomic , strong) NSArray<MSUDetailGoodsModel> *goods;
        /// 动态视频
        @property (nonatomic , strong) NSArray<MSUDetailVideoModel> *video;
        /// 评论
        @property (nonatomic , strong) NSArray<MSUArrModel> *comments;
        /// 自己id
        @property (nonatomic , assign)NSInteger my_id;
        
        
        
        @end
        
        #pragma makr - 视频
        @interface MSUDetailVideoModel : NSObject
        
        /// 作者id
        @property (nonatomic , assign) NSInteger _id;
        
        /// 作者头像
        @property (nonatomic , copy) NSString *author_avatar;
        
        /// 作者名字
        @property (nonatomic , copy) NSString *author_nickname;
        
        /// 评论数量
        @property (nonatomic , assign) NSInteger comment_num;
        
        /// 转发人头像
        @property (nonatomic , copy) NSString *d_avatar;
        
        /// 转发人名字
        @property (nonatomic , copy) NSString *d_nickname;
        
        /// 转发人id
        @property (nonatomic , assign) NSInteger d_userid;
        
        /// 转发人的评论
        @property (nonatomic , copy) NSString *describe;
        
        /// 动态的id
        @property (nonatomic , assign) NSInteger dynamic_state_id;
        
        /// 转发数量
        @property (nonatomic , assign) NSInteger forward_num;
        
        /// 是否关注
        @property (nonatomic , assign) NSInteger is_followed;
        
        /// 是否收藏
        @property (nonatomic , assign) NSInteger is_collected;
        
        /// 是否转发
        @property (nonatomic , assign) NSInteger is_relay;
        
        /// 是否举报
        @property (nonatomic , assign) NSInteger is_tipped;
        
        /// 经度
        @property (nonatomic , copy) NSString *latitude;
        
        /// 维度
        @property (nonatomic , copy) NSString *longitude;
        
        /// 地名
        @property (nonatomic , copy) NSString *place_name;
        
        /// 点赞数量
        @property (nonatomic , assign) NSInteger upvote;
        
        /// 是否点赞
        @property (nonatomic , assign) NSInteger is_liked;
        
        /// 评论内容
        @property (nonatomic , copy) NSString *video_brife;
        
        /// 视频id
        @property (nonatomic , assign) NSInteger video_id;
        
        /// 发布时间
        @property (nonatomic , copy) NSString *video_upload_time;
        
        /// 视频封面图
        @property (nonatomic , copy) NSString *video_img;
        
        /// 视频点赞人数
        @property (nonatomic , assign) NSInteger video_like;
        
        /// 视频url
        @property (nonatomic , copy) NSString *video_source_url;
        
        /// 标签时间
        @property (nonatomic , strong) NSArray<MSUSignDetailModel> *sign;
        
        @end
        
        @interface MSUSignDetailModel : NSObject
        
        @property (nonatomic , copy) NSString *name;
        
        @property (nonatomic , copy) NSString *time;
        
        @end
        
        
        #pragma mark  - 商品
        @interface MSUDetailGoodsModel : NSObject
        
        /// 商品id
        @property (nonatomic , copy) NSString *goods_id;
        
        /// 商品名字
        @property (nonatomic , copy) NSString *goods_name;
        
        /// 商品图片
        @property (nonatomic , copy) NSString *goods_pic;
        
        /// 商品价格
        @property (nonatomic , assign) NSString *goods_price;
        
        /// id
        @property (nonatomic , copy) NSString *_id;
        
        /// 卖家头像
        @property (nonatomic , copy) NSString *avatar;
        
        /// 结束时间
        @property (nonatomic , copy) NSString *goods_expire_time;
        
        /// 开始时间
        @property (nonatomic , copy) NSString *goods_start_time;
        
        /// 卖家名字
        @property (nonatomic , copy) NSString *nickname;
        
        /// 推广佣金
        @property (nonatomic , copy) NSString *spread_money;
        
        /// 购买商品结算佣金
        @property (nonatomic , copy) NSString *video_brokerage;
        
        /// 是否是推广商品
        @property (nonatomic , assign) NSInteger is_popularize;
        
        @end
        
        
        #pragma makr - 评论
        @interface MSUArrModel : NSObject
        
        /// 评论id
        @property (nonatomic , copy) NSString *_id;
        
        /// 评论内容
        @property (nonatomic , copy) NSString *content;
        
        /// 评论者的头像
        @property (nonatomic , copy) NSString *from_avatar;
        
        /// 评论者的id
        @property (nonatomic , copy) NSString *from_uid;
        
        /// 评论者的昵称
        @property (nonatomic , copy) NSString *from_user;
        
        /// 时间戳
        @property (nonatomic , copy) NSString *time;
        
        /// 点赞数
        @property (nonatomic , copy) NSString *upvote;
        
        /// 是否点赞
        @property (nonatomic , assign) NSInteger is_liked;
        
        /// 是否恢复  1为评论 2为回复
        @property (nonatomic , copy) NSString *topic_type;
        
        /// 回复
        @property (nonatomic , strong) NSArray<MSUCommentModel> *reply;
        
        @end
        
        
        @interface MSUCommentModel : NSObject
        
        /// 评论id
        @property (nonatomic , copy) NSString *_id;
        
        /// 内容
        @property (nonatomic , copy) NSString *content;
        
        /// 评论者的头像
        @property (nonatomic , copy) NSString *from_avatar;
        
        /// 评论者的id
        @property (nonatomic , copy) NSString *from_uid;
        
        /// 评论者的昵称
        @property (nonatomic , copy) NSString *from_user;
        
        /// 时间戳
        @property (nonatomic , copy) NSString *time;
        
        /// 被评论者的头像
        @property (nonatomic , copy) NSString *to_avatar;
        
        /// 被评论的评论id
        @property (nonatomic , copy) NSString *to_comment_id;
        
        /// 被评论的评论者id
        @property (nonatomic , copy) NSString *to_uid;
        
        /// 被评论的评论者昵称
        @property (nonatomic , copy) NSString *to_user;
        
        /// 被评论时,首条评论的id
        @property (nonatomic , copy) NSString *topic_id;
        
        /// 是否恢复  1为评论 2为回复
        @property (nonatomic , copy) NSString *topic_type;
        
        /// 点赞数量
        @property (nonatomic , copy) NSString *upvote;
        
        ///
        
        @end

    2.MSUVideoDetailModel.m中的代码

        #import "MSUVideoDetailModel.h"

        @implementation MSUVideoDetailModel
        
        + (NSDictionary *)mj_objectClassInArray{
            return @{@"data":[MSUDataDetailModel class]};
        }
        
        @end
        
        @implementation MSUDataDetailModel
        
        + (NSDictionary *)mj_objectClassInArray{
            return @{@"video":[MSUDetailVideoModel class],
                     @"goods":[MSUDetailGoodsModel class],
                     @"comments":[MSUArrModel class]};
        }
        
        @end
        
        
        #pragma makr - 视频
        @implementation MSUDetailVideoModel
        
        + (NSDictionary *)mj_objectClassInArray{
            return @{@"sign":[MSUSignDetailModel class]};
        }
        
        @end
        
        @implementation MSUSignDetailModel
        
        
        @end
        
        
        #pragma makr - 商品
        @implementation MSUDetailGoodsModel
        
        
        @end
        
        
        #pragma makr - 评论
        @implementation MSUCommentModel
        
        @end
        
        @implementation MSUArrModel
        
        + (NSDictionary *)mj_objectClassInArray{
            return @{@"reply":[MSUCommentModel class]};
        }
        
        @end

二、评论的核心结构就是 tableview 嵌套 tableview ,所以,新建一个View 集成UIView MSUVideoDetailCommentView

1. MSUVideoDetailCommentView.h 中的代码

        #import <UIKit>
        #import "MSUVideoDetailModel.h"
        
        @protocol MSUVideoDetailCommentViewDelegate <NSObject>
        
        - (void)pinglunCellDidSelectWithUid:(NSString *)uid commentID:(NSString *)commentID topId:(NSString *)topID;
        
        @end
        
        @interface MSUVideoDetailCommentView : UIView
        
        @property (nonatomic , weak) id<MSUVideoDetailCommentViewDelegate> delegate;
        
        /// 回调table高度
        @property (nonatomic , copy)void(^tableHeightBlock)(NSInteger height);
        
        /// 评论标题
        @property (nonatomic , strong) UILabel *titLab;
        
        /// 内容
        @property (nonatomic , strong) UITableView *tableView;
        
        /// 无内容视图
        @property (nonatomic , strong) UILabel * comeLab;
        
        /// 数据
        @property (nonatomic , strong) MSUDataDetailModel *dataModel;
        
        /// 行高
        @property (nonatomic , assign) CGFloat cellHeight;
        
        
        @end

    2. MSUVideoDetailCommentView.m 中的代码 (MSUPathTools.h 是我封装的路径类,MSUStringTools.h是封装的string相关类,在这个地方用以获取文本高度,MSUAFNRequest.h是网络请求类,MSUHUD.h是弹窗类,可根据自己封装类使用)

            #import "MSUVideoDetailCommentView.h"

            #import "UIButton+WebCache.h"
            #import "MSUPathTools.h"
            #import "MSUStringTools.h"
            #import "MSUAFNRequest.h"
            #import "MSUHUD.h"
            
            #define WIDTH [UIScreen mainScreen].bounds.size.width
            
            #define SelfWidth [UIScreen mainScreen].bounds.size.width
            
            //masonry
            #define MAS_SHORTHAND
            #define MAS_SHORTHAND_GLOBALS
            #import "Masonry.h"
            
            #define HEXCOLOR(rgbValue)      [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
            
            #import "MSUVideoCommentTableCell.h"
            
            @interface MSUVideoDetailCommentView ()<UITableViewDelegate>
            
            @end
            
            @implementation MSUVideoDetailCommentView
            
            - (instancetype)initWithFrame:(CGRect)frame
            {
                if (self = [super initWithFrame:frame]) {
                    
                    [self createView];
                    
                }
                return self;
            }
            
            - (void)setDataModel:(MSUDataDetailModel *)dataModel{
                _dataModel = dataModel;
                
                if (_dataModel.comments.count > 0) {
                    self.titLab.text = [NSString stringWithFormat:@"评论(%ld)",_dataModel.comments.count];
                    self.comeLab.hidden = YES;
                    self.tableView.hidden = NO;
                } else{
                    self.titLab.text = @"评论";
                    self.tableView.hidden = YES;
                    self.comeLab.hidden = NO;
                }
                
                CGFloat height = 0.0;
                for (NSInteger i = 0; i < _dataModel.comments.count; i++) {
                    MSUArrModel *arrModel = _dataModel.comments[i];
                    // 计算评论内容高度
                    CGRect rect = [MSUStringTools danamicGetHeightFromText:arrModel.content WithWidth:SelfWidth-65-14 font:11];
                    
                    // 计算回复table高度
                    CGFloat tableViewHeight = 0.0;
                    for (NSInteger i = 0; i < arrModel xss=removed> 0) {
                            str = [NSString stringWithFormat:@"%@ 回复 %@:%@",commentModel.from_user,commentModel.to_user,commentModel.content];;
                        }else {
                            str = [NSString stringWithFormat:@"%@:%@",commentModel.from_user,commentModel.content];
                        }
                        CGRect comnmentRect = [MSUStringTools danamicGetHeightFromText:str WithWidth:SelfWidth-28-36-5-20 font:10];
                        tableViewHeight = tableViewHeight + comnmentRect.size.height + 5;
                    }
                    
                    // 计算评论table高度
                   height = height + 28 + rect.size.height + 5 + tableViewHeight + 15 + 10;
                }
                
                // 重设评论table高度
                [_tableView remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.top).offset(35);
                    make.left.equalTo(self.left).offset(0);
                    make.width.equalTo(SelfWidth);
                    make.height.equalTo(height);
                }];
                
                [self.tableView reloadData];
                
                if (self.tableHeightBlock) {
                    self.tableHeightBlock(height);
                }
            }
            
            
            - (void)createView{
                UIView *navView = [[UIView alloc] init];
                navView.backgroundColor = [UIColor whiteColor];
                [self addSubview:navView];
                [navView makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.top).offset(0);
                    make.left.equalTo(self.left).offset(0);
                    make.width.equalTo(SelfWidth);
                    make.height.equalTo(50);
                }];
                
                // 标题 评论
                self.titLab = [[UILabel alloc] init];
            //    _titLab.backgroundColor = [UIColor redColor];
                _titLab.font = [UIFont systemFontOfSize:14];
                _titLab.textColor = HEXCOLOR(0x333333);
                [navView addSubview:_titLab];
                [_titLab makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.top).offset(15);
                    make.left.equalTo(self.left).offset(14);
                    make.width.equalTo(SelfWidth*0.5);
                    make.height.equalTo(20);
                }];
                
                
                // tableview
                self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
                _tableView.backgroundColor = [UIColor whiteColor];
                _tableView.showsVerticalScrollIndicator = NO;
                _tableView.showsHorizontalScrollIndicator = NO;
                _tableView.scrollEnabled = NO;
            //    _tableView.userInteractionEnabled = NO;
                [self addSubview:_tableView];
                [_tableView registerClass:[MSUVideoCommentTableCell class] forCellReuseIdentifier:@"commentVideoShopCell"];
                self.tableView.delegate = self;
                self.tableView.dataSource = self;
                self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                
                self.comeLab = [[UILabel alloc] init];
                _comeLab.backgroundColor = [UIColor clearColor];
                _comeLab.text = @"快来发表你的评论吧~";
                _comeLab.textAlignment = NSTextAlignmentCenter;
                _comeLab.font = [UIFont systemFontOfSize:11];
                _comeLab.textColor = HEXCOLOR(0xf49418);
                [self addSubview:_comeLab];
                [_comeLab makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(navView.bottom).offset(10);
                    make.left.equalTo(self.left).offset(0);
                    make.width.equalTo(SelfWidth);
                    make.height.equalTo(20);
                }];
                _comeLab.hidden = YES;
            }
            
            - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
                return _dataModel.comments.count;
            }
            
            - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
                // 避免出现cellForRowAtIndexPath中的indexPath.row从4或5开始走,默认设置个初始值
                if (self.cellHeight) {
                    return self.cellHeight;
                } else{
                    return 2;
                }
            }
            
            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                MSUVideoCommentTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentVideoShopCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                MSUArrModel *arrModel = _dataModel.comments[indexPath.row];
                
                [cell.iconBtn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://source.showbuy100.com%@",arrModel.from_avatar]] forState:UIControlStateNormal placeholderImage:[MSUPathTools showImageWithContentOfFileByName:@"search-headbig"]];
                cell.nickLab.text = arrModel.from_user;
                cell.commentLab.text = arrModel.content;
                CGRect rect = [MSUStringTools danamicGetHeightFromText:arrModel.content WithWidth:SelfWidth-65-14 font:11];
                cell.commentLab.frame = CGRectMake(55, 28, SelfWidth-55-14, rect.size.height);
                
                // 评论
                cell.pingBtnClickBlock = ^(UIButton *btn) {
                    if (self.delegate && [self.delegate respondsToSelector:@selector(pinglunCellDidSelectWithUid:commentID:topId:)]) {
                        [self.delegate pinglunCellDidSelectWithUid:arrModel.from_uid commentID:arrModel._id topId:arrModel._id];
                    }
                };
                
                // 点赞
                if (arrModel.upvote.length > 0 && [arrModel.upvote integerValue] > 0) {
                    cell.likeLab.hidden = NO;
                    cell.likeLab.text = arrModel.upvote;
                    CGSize sizea = [MSUStringTools danamicGetWidthFromText:cell.likeLab.text WithFont:11];
                    cell.likeLab.frame = CGRectMake(SelfWidth-14-15-25, 10, sizea.width, 15);
                    
                    cell.likeBtn.frame = CGRectMake(SelfWidth-14-15-25-sizea.width-15, 10, 15, 15);
                } else{
                    cell.likeLab.hidden = YES;
                    cell.likeBtn.frame = CGRectMake(SelfWidth-14-15-30, 10, 15, 15);
                }
                
                if (arrModel.is_liked) {
                    [cell.likeBtn setTitle:arrModel.upvote forState:UIControlStateNormal];
                    cell.likeBtn.selected = YES;
                }else{
                    cell.likeBtn.selected = NO;
                    __weak typeof(cell) weakCell = cell;
                    cell.likeBtnClickBlock = ^(UIButton *btn) {
                        btn.selected = YES;
                        weakCell.likeLab.hidden = NO;
                        weakCell.likeLab.text = [NSString stringWithFormat:@"%ld",[arrModel.upvote integerValue]+1];
                        CGSize sizea = [MSUStringTools danamicGetWidthFromText:weakCell.likeLab.text WithFont:11];
                        weakCell.likeLab.frame = CGRectMake(SelfWidth-14-15-25, 10, sizea.width, 15);
                        weakCell.likeBtn.frame = CGRectMake(SelfWidth-14-15-25-sizea.width-15, 10, 15, 15);
                        
                        NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
                        token = token?token:@"";
                        NSDictionary *dic = @{@"token":token,@"_id":arrModel._id};
                        [[MSUAFNRequest sharedInstance] postRequestWithURL:@"http://api.showbuy100.com/index/comment-up" parameters:dic withBlock:^(id obj, NSError *error) {
                            if (obj) {
                                NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingMutableLeaves error:nil];
                                if (!error) {
                                    NSLog(@"访问成功%@",jsonDict);
                                    btn.userInteractionEnabled = NO;
                                }else{
                                    NSLog(@"访问报错%@",error);
                                }
                            } else{
                                [MSUHUD showFileWithString:@"服务器请求为空"];
                            }
                           
                        }];
            
                       
                    };
                }
                
                // 计算回复table 高度
                CGFloat tableViewHeight = 0.0;
                if (arrModel.reply.count > 0) {
                    for (NSInteger i = 0; i < arrModel xss=removed> 0) {
                            str = [NSString stringWithFormat:@"%@ 回复 %@:%@",commentModel.from_user,commentModel.to_user,commentModel.content];;
                        }else {
                            str = [NSString stringWithFormat:@"%@:%@",commentModel.from_user,commentModel.content];
                        }
                        CGRect comnmentRect = [MSUStringTools danamicGetHeightFromText:str WithWidth:SelfWidth-28-36-5-20 font:10];
                        tableViewHeight = tableViewHeight + comnmentRect.size.height + 5;
                    }
                } else{
                    tableViewHeight = -15;
                }
                
                // 加上头部视图和底部试图
                [cell configCellWithModel:arrModel tableHeight:tableViewHeight+15];
            
                cell.lineView.frame = CGRectMake(55, CGRectGetMaxY(cell.commentLab.frame)+5+tableViewHeight+15+10-1, SelfWidth-55-14, 1);
            
                
                self.cellHeight = CGRectGetMaxY(cell.lineView.frame);
                cell.delegate = self;
                
                return cell;
            }
            
            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
                MSUArrModel *arrModel = _dataModel.comments[indexPath.row];
            
                NSLog(@"评论cell");
                if (self.delegate && [self.delegate respondsToSelector:@selector(pinglunCellDidSelectWithUid:commentID:topId:)]) {
                    [self.delegate pinglunCellDidSelectWithUid:arrModel.from_uid commentID:arrModel._id topId:arrModel._id];
                }
            }
            
            - (void)replyCellDidSelectWithUid:(NSString *)uid commentID:(NSString *)commentID topId:(NSString *)topID{
                if (self.delegate && [self.delegate respondsToSelector:@selector(pinglunCellDidSelectWithUid:commentID:topId:)]) {
                    [self.delegate pinglunCellDidSelectWithUid:uid commentID:commentID topId:topID];
                }
            }
            
            @end

三、MSUVideoCommentTableCell 内容,回复tableview创建就是在 这个cell里面

    1. MSUVideoCommentTableCell.h 中的代码

                
        #import <UIKit>
        #import "MSUVideoDetailModel.h"
        
        @protocol MSUVideoCommentTableCellDelegate <NSObject>
        
        - (void)replyCellDidSelectWithUid:(NSString *)uid commentID:(NSString *)commentID topId:(NSString *)topID;
        
        @end
        
        @interface MSUVideoCommentTableCell : UITableViewCell
        
        @property (nonatomic , weak) id<MSUVideoCommentTableCellDelegate> delegate;
        
        /// 点赞按钮回调
        @property (nonatomic , copy) void(^likeBtnClickBlock)(UIButton *btn);
        /// 评论按钮回调
        @property (nonatomic , copy) void(^pingBtnClickBlock)(UIButton *btn);
        
        /// 头像
        @property (nonatomic , strong) UIButton *iconBtn;
        
        /// 昵称
        @property (nonatomic , strong) UILabel *nickLab;
        
        /// 评论
        @property (nonatomic , strong) UIButton *commentBtn;
        
        /// 点赞
        @property (nonatomic , strong) UIButton *likeBtn;
        @property (nonatomic , strong) UILabel *likeLab;
        
        /// 内容
        @property (nonatomic , strong) UILabel *commentLab;
        
        /// 背景线
        @property (nonatomic , strong) UIView *lineView;
        
        /// 评论列表
        @property (nonatomic , strong) UITableView *commentTableView;
        
        /// 数据
        @property (nonatomic , strong) MSUArrModel *arrModel;
        
        /// 行高
        @property (nonatomic , assign) CGFloat cellHeight;
        
        - (void)configCellWithModel:(MSUArrModel *)model tableHeight:(NSInteger )height;
        
        
        @end

    2. MSUVideoCommentTableCell.m 中的代码

        #import "MSUVideoCommentTableCell.h"
        #import "MSUCommentTextTableCell.h"
        
        #import "MSUStringTools.h"
        
        //masonry
        #define MAS_SHORTHAND
        #define MAS_SHORTHAND_GLOBALS
        #import "Masonry.h"
        
        #define SelfWidth [UIScreen mainScreen].bounds.size.width
        #define CommentTableWidth SelfWidth-28-36-5
        
        #define HEXCOLOR(rgbValue)      [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
        
        @interface MSUVideoCommentTableCell()<UITableViewDataSource>
        
        @end
        
        @implementation MSUVideoCommentTableCell
        
        - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
            if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
                [self createView];
            }
            return self;
        }
        
        - (void)createView{
            // 头像
            self.iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _iconBtn.backgroundColor = HEXCOLOR(0xfe4e17);
            _iconBtn.layer.cornerRadius = 18;
            _iconBtn.clipsToBounds = YES;
            _iconBtn.layer.shouldRasterize = YES;
            _iconBtn.layer.rasterizationScale = [UIScreen mainScreen].scale;
            [self addSubview:_iconBtn];
            [_iconBtn makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.top).offset(10);
                make.left.equalTo(self.left).offset(14);
                make.width.equalTo(36);
                make.height.equalTo(36);
            }];
            
            // 昵称
            self.nickLab = [[UILabel alloc] init];
            _nickLab.font = [UIFont systemFontOfSize:13];
            //    _nickLab.backgroundColor = [UIColor brownColor];
            [self addSubview:_nickLab];
            [_nickLab makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.top).offset(10);
                make.left.equalTo(_iconBtn.right).offset(5);
                make.width.equalTo(SelfWidth * 0.5);
                make.height.equalTo(10);
            }];
            
            // 评论
            self.commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_commentBtn setImage:[UIImage imageNamed:@"state-comment"] forState:UIControlStateNormal];
            _commentBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self addSubview:_commentBtn];
            [_commentBtn makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_nickLab.top).offset(0);
                make.right.equalTo(self.right).offset(-14);
                make.width.equalTo(15);
                make.height.equalTo(15);
            }];
            [_commentBtn addTarget:self action:@selector(commentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
            // 点赞
            self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_likeBtn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
            [_likeBtn setImage:[UIImage imageNamed:@"WechatIMG1191"] forState:UIControlStateSelected];
            _likeBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self addSubview:_likeBtn];
        //    [_likeBtn makeConstraints:^(MASConstraintMaker *make) {
        //        make.top.equalTo(_nickLab.top).offset(0);
        //        make.right.equalTo(_commentBtn.left).offset(-20);
        //        make.width.equalTo(12.5);
        //        make.height.equalTo(12.5);
        //    }];
            [_likeBtn addTarget:self action:@selector(likeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            // 正文
            self.likeLab = [[UILabel alloc] init];
            [_likeLab setTextColor:HEXCOLOR(0x333333)];
            _likeLab.textAlignment = NSTextAlignmentRight;
            //    _timeLab.backgroundColor = [UIColor redColor];
            _likeLab.font = [UIFont systemFontOfSize:11];
            [self addSubview:_likeLab];
            _likeLab.hidden = YES;
            
            // 正文
            self.commentLab = [[UILabel alloc] init];
            [_commentLab setTextColor:HEXCOLOR(0x333333)];
            //    _timeLab.backgroundColor = [UIColor redColor];
            _commentLab.font = [UIFont systemFontOfSize:11];
            _commentLab.numberOfLines = 0;
            [self addSubview:_commentLab];
            
            self.commentTableView = [[UITableView alloc] init];
            _commentTableView.backgroundColor = HEXCOLOR(0xf4f4f4);
            _commentTableView.dataSource = self;
            _commentTableView.delegate = self;
            [self addSubview:_commentTableView];
            _commentTableView.scrollEnabled = NO;
        //    _commentTableView.userInteractionEnabled = NO;
            _commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            [_commentTableView registerClass:[MSUCommentTextTableCell class] forCellReuseIdentifier:@"commentText1Cell"];
            
            self.lineView = [[UIView alloc] init];
            _lineView.backgroundColor = HEXCOLOR(0xf4f4f4);
            [self addSubview:_lineView];
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CommentTableWidth, 7.5)];
            view.backgroundColor = HEXCOLOR(0xf4f4f4);
            _commentTableView.tableHeaderView = view;
            _commentTableView.tableFooterView = view;
            
        }
        
        - (void)configCellWithModel:(MSUArrModel *)model tableHeight:(NSInteger)height{
            self.arrModel = model;
        
            // 重设 回复table高度
            [_commentTableView remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_commentLab.bottom).offset(5);
                make.left.equalTo(_commentLab.left).offset(0);
                make.width.equalTo(CommentTableWidth);
                make.height.equalTo(height);
            }];
            
            [self.commentTableView reloadData];
        }
        
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
            return self.arrModel.reply.count;
        }
        
        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
            return self.cellHeight;
        }
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
            MSUCommentTextTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentText1Cell"];
            cell.backgroundColor = HEXCOLOR(0xf4f4f4);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            // 回复内容
            MSUCommentModel *commentModel = self.arrModel.reply[indexPath.row];
            if (commentModel.to_user.length > 0) {
                // 富文本设置
                NSString *str = [NSString stringWithFormat:@"%@ 回复 %@:%@",commentModel.from_user,commentModel.to_user,commentModel.content];;
                NSMutableAttributedString *att = [MSUStringTools makeKeyWordAttributedWithSubText:commentModel.to_user inOrigiText:str font:10 color:HEXCOLOR(0xf49418)];
                cell.commentLab.attributedText = [MSUStringTools makeAttrubuteKeyWordAttributedWithSubText:commentModel.from_user inOrigiText:att font:10 color:HEXCOLOR(0xf49418)];
            }else {
                // 富文本设置
                NSString *str = [NSString stringWithFormat:@"%@:%@",commentModel.from_user,commentModel.content];
                cell.commentLab.attributedText = [MSUStringTools makeKeyWordAttributedWithSubText:commentModel.from_user inOrigiText:str font:10 color:HEXCOLOR(0xf49418)];
            }
            
            // 计算回复内容高
            CGRect comnmentRect = [MSUStringTools danamicGetHeightFromText:cell.commentLab.text WithWidth:CommentTableWidth-20 font:10];
            cell.commentLab.frame = CGRectMake(10, 2.5, CommentTableWidth-20, comnmentRect.size.height);
        
            // cell高度
            self.cellHeight = comnmentRect.size.height + 5;
            
            return cell;
        }
        
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
            MSUCommentModel *commentModel = self.arrModel.reply[indexPath.row];
        
            NSLog(@"回复cell");
            if (self.delegate && [self.delegate respondsToSelector:@selector(replyCellDidSelectWithUid:commentID:topId:)]) {
                [self.delegate replyCellDidSelectWithUid:commentModel.from_uid commentID:commentModel._id topId:self.arrModel._id];
            }
        }
        
        #pragma mark - 点击事件
        - (void)likeBtnClick:(UIButton *)sender{
            if (self.likeBtnClickBlock) {
                self.likeBtnClickBlock(sender);
            }
        }
        
        - (void)commentBtnClick:(UIButton *)sender{
            if (self.pingBtnClickBlock) {
                self.pingBtnClickBlock(sender);
            }
        }
        
        @end

四、回复tableview 的 cell ,MSUCommentTextTableCell

    1.MSUCommentTextTableCell.h 中的代码

        #import <UIKit>

        @interface MSUCommentTextTableCell : UITableViewCell
        
        /// 评论内容
        @property (nonatomic , strong) UILabel *commentLab;
        
        @end

    2.MSUCommentTextTableCell.m 中的内容

        #import "MSUCommentTextTableCell.h"

        //masonry
        #define MAS_SHORTHAND
        #define MAS_SHORTHAND_GLOBALS
        #import "Masonry.h"
        
        
        #define HEXCOLOR(rgbValue)      [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
        
        @implementation MSUCommentTextTableCell
        
        - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
            if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
                [self createContentView];
            }
            return self;
        }
        
        - (void)createContentView{
            UIView *bgView = [[UIView alloc] init];
            [self addSubview:bgView];
            [bgView makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.top).offset(0);
                make.left.equalTo(self.left).offset(0);
                make.width.equalTo(self.frame.size.width);
                make.height.equalTo(2.5);
            }];
            
            self.commentLab = [[UILabel alloc] init];
            _commentLab.font = [UIFont systemFontOfSize:10];
            _commentLab.textColor = HEXCOLOR(0x333333);
            [self addSubview:_commentLab];
            
            UIView *bg1View = [[UIView alloc] init];
            [self addSubview:bg1View];
            [bg1View makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_commentLab.bottom).offset(0);
                make.left.equalTo(self.left).offset(0);
                make.width.equalTo(self.frame.size.width);
                make.height.equalTo(2.5);
            }];
            
        }
        
        @end
上一篇下一篇

猜你喜欢

热点阅读