仿微信评论回复(简易)

简单的微信评论回复功能

先说说大概逻辑,一个控制器里添加列表(类似朋友圈的每条动态),每个动态里面再添加一个列表(用来显示所有评论以及点击回复评论),都是用Masonry布局加HYBMasonryAutoCellHeight自适应行高来完成,键盘是随便找的一个第三方,而且项目里也没重点设置

先看看大概的界面以及两个模型里面的属性

1.png
2.png
3.png

下面是viewController里面代码,这里把回复的人的名字定死了,有数据的话可以根据实际情况来定

#import "ViewController.h"
#import "Masonry.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"
#import "TableViewCell.h"
#import "ComentModel.h"
#import "AllComentModel.h"

#import "ChatKeyBoard.h"
#define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,ChatKeyBoardDelegate,TableviewCellDelegate>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSArray *allMessage;

@property (nonatomic, strong) ChatKeyBoard *chatKeyBoard;

@property (nonatomic, strong) NSIndexPath *myIndexPath;

//回复给谁
@property (nonatomic, strong) NSString *name;

@end

@implementation ViewController

-(ChatKeyBoard *)chatKeyBoard{
    if (_chatKeyBoard==nil) {
        _chatKeyBoard =[ChatKeyBoard keyBoardWithNavgationBarTranslucent:YES];
        _chatKeyBoard.delegate = self;
        _chatKeyBoard.keyBoardStyle = KeyBoardStyleComment;
        _chatKeyBoard.allowVoice = NO;
        _chatKeyBoard.allowMore = NO;
        _chatKeyBoard.allowFace = NO;
        _chatKeyBoard.allowSwitchBar = NO;
        _chatKeyBoard.placeHolder = @"评论";
        [self.view addSubview:_chatKeyBoard];
        [self.view bringSubviewToFront:_chatKeyBoard];
    }
    return _chatKeyBoard;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];

    AllComentModel *coment = [AllComentModel new];
    
    ComentModel *model = [ComentModel new];
    model.startPeople = @"马云";
    model.remarkText = @"钱太多花不完怎么办";
    model.remarkPeople = @"";
    ComentModel *model1 = [ComentModel new];
    model1.startPeople = @"大师兄";
    model1.remarkText = @"真羡慕你们这么年纪轻轻就认识像我这么有才华的人";
    coment.allComents = @[model,model1];
    model1.remarkPeople = @"";
    AllComentModel *coment1 = [AllComentModel new];
    
    coment1.allComents = @[model,model1];
    
    self.allMessage = @[coment,coment1];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.chatKeyBoard keyboardUpforComment];
}

#pragma mark --
#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allMessage.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.delegate = self;
    }
    [cell configCellWithModel:self.allMessage[indexPath.row] indexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [TableViewCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
        TableViewCell *cell = (TableViewCell *)sourceCell;
        [cell configCellWithModel:self.allMessage[indexPath.row] indexPath:indexPath];
    }];
}

#pragma mark -- TableViewCellDelegate
- (void)clickCellWithModel:(ComentModel *)model andIndex:(NSIndexPath *)indexPath {
    if (![model.remarkPeople isEqualToString:@""]) {
        self.chatKeyBoard.placeHolder = [NSString stringWithFormat:@"回复%@:",model.remarkPeople];
    }else {
        self.chatKeyBoard.placeHolder = [NSString stringWithFormat:@"回复%@:",model.startPeople];
    }
    self.name = model.startPeople;
    [self.chatKeyBoard keyboardUpforComment];
    self.myIndexPath = indexPath;
}

//发送
- (void)chatKeyBoardSendText:(NSString *)text{
    [self.chatKeyBoard keyboardDownForComment];
    
    AllComentModel *model = self.allMessage[self.myIndexPath.row];
//评论人model,评论人名写死了
    ComentModel *commodel = [ComentModel new];
    commodel.startPeople = self.name;
    commodel.remarkText = text;
    commodel.remarkPeople = @"马化腾";
    NSMutableArray *mtAry = [NSMutableArray arrayWithArray:model.allComents];
    [mtAry addObject:commodel];
    model.allComents = mtAry.mutableCopy;
    
    [self.tableview reloadRowsAtIndexPaths:@[self.myIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}

下面是第一个tableViewCell的.h和.m

#import <UIKit/UIKit.h>
#import "ComentModel.h"
#import "AllComentModel.h"

@class TableViewCell;

@protocol TableviewCellDelegate <NSObject>

- (void)clickCellWithModel:(ComentModel *)model andIndex:(NSIndexPath *)indexPath;

@end

@interface TableViewCell : UITableViewCell

@property (nonatomic, weak) id<TableviewCellDelegate> delegate;

@property (nonatomic, strong) UITableView *tableView;

- (void)configCellWithModel:(AllComentModel *)model indexPath:(NSIndexPath *)indexPath;

@end
#import "TableViewCell.h"
#import "Masonry.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"
#import "MessageCell.h"
#import "AllComentModel.h"

#define kGAP 10
@interface TableViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSIndexPath *indexPath;

@property (nonatomic, strong) AllComentModel *allComents;

@end

@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor lightGrayColor];
        
        self.tableView = [[UITableView alloc] init];
        
        self.tableView.scrollEnabled = NO;
        [self.contentView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kGAP);
            make.top.mas_equalTo(kGAP);
            make.right.mas_equalTo(-kGAP);
        }];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.hyb_lastViewInCell = self.tableView;
        self.hyb_bottomOffsetToCell = 0.0;
    }
    return self;
}

- (void)configCellWithModel:(AllComentModel *)model indexPath:(NSIndexPath *)indexPath {
    CGFloat tableviewHeight = 0;
    self.allComents = model;
    self.indexPath = indexPath;
    
    for (ComentModel *comentModel in model.allComents) {
        CGFloat cellheight = [MessageCell hyb_heightForTableView:self.tableView config:^(UITableViewCell *sourceCell) {
            MessageCell *cell = (MessageCell *)sourceCell;
            [cell configCellWithModel:comentModel];
        }];
        tableviewHeight += cellheight;
    }
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(tableviewHeight);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}

#pragma mark --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allComents.allComents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    ComentModel *model = self.allComents.allComents[indexPath.row];
    [cell configCellWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ComentModel *model = self.allComents.allComents[indexPath.row];
    CGFloat cell_height = [MessageCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
        MessageCell *cell = (MessageCell *)sourceCell;
        [cell configCellWithModel:model];
    } cache:^NSDictionary *{
        NSDictionary *cache = @{kHYBCacheUniqueKey : @"",
                                kHYBCacheStateKey : @"",
                                kHYBRecalculateForStateKey : @(YES)};
        return cache;
    }];
    return cell_height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //当点击到自己回复的语句时,微信是弹出删除功能,需要的也可以自己加上去
    ComentModel *model = self.allComents.allComents[indexPath.row];
    if ([model.remarkPeople isEqualToString:@"马化腾"]) {
        NSLog(@"点击了自己的回复");
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(clickCellWithModel:andIndex:)]) {
        [self.delegate clickCellWithModel:model andIndex:self.indexPath];
    }
}

下面是主列表里面包含的评论列表的.h和.m

#import <UIKit/UIKit.h>
#import "ComentModel.h"

@interface MessageCell : UITableViewCell

@property (nonatomic, strong) UILabel *contentLabel;

- (void)configCellWithModel:(ComentModel *)model;

@end

/**如果想做类似微信那样点击评论人名和回复人名跳转的话可以在这里写,我这里总的写成了一个label*/

#import "MessageCell.h"
#import "Masonry.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"

@implementation MessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // contentLabel
        self.contentLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 80;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:14.0];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.contentView);
            make.top.mas_equalTo(self.contentView).offset(3.0);
        }];
        self.hyb_lastViewInCell = self.contentLabel;
        self.hyb_bottomOffsetToCell = 3.0;
    }
    return self;
}

- (void)configCellWithModel:(ComentModel *)model {
    NSString *str = nil;
    if (![model.remarkPeople isEqualToString:@""]) {
        str = [NSString stringWithFormat:@"%@ 回复 %@: %@",model.remarkPeople,model.startPeople,model.remarkText];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor orangeColor]
                     range:NSMakeRange(0, model.remarkPeople.length)];
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor orangeColor]
                     range:NSMakeRange(model.remarkPeople.length + 4, model.startPeople.length+1)];
        self.contentLabel.attributedText = text;
    }else {
        str = [NSString stringWithFormat:@"%@: %@",model.startPeople,model.remarkText];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        [text addAttribute:NSForegroundColorAttributeName
                     value:[UIColor orangeColor]
                     range:NSMakeRange(0, model.startPeople.length+1)];
        self.contentLabel.attributedText = text;
    }
}

大致效果是这样的

xxxx.gif

代码写的比较仓促,有什么写的不对的大家可以评论,哈哈
有没有大神愿意加我个小群,自己建的,只有3个人,都比较菜,如果哪位大神平时喜欢带新人的话加我群:515385179 哈哈

另外加个广告,推荐几个自己GitHub项目,希望多几个星星

UILabel分类,使用简单,动画改变label数值
对极光推送和信鸽推送的封装,统一调用,简单易懂
对MJRefresh二次封装,让代码更清晰
封装的一个二维码扫描器
登陆、支付、分享(待完善)功能封装
这是我的GitHub首页

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容