简单的微信评论回复功能

先说说大概逻辑,一个控制器里添加列表(类似朋友圈的每条动态),每个动态里面再添加一个列表(用来显示所有评论以及点击回复评论),都是用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 ()

@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 *)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

#import "ComentModel.h"

#import "AllComentModel.h"

@class TableViewCell;

@protocol TableviewCellDelegate

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

@end

@interface TableViewCell : UITableViewCell

@property (nonatomic, weak) id 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 ()

@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

#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项目,希望多几个星星

android 仿 好友动态回复,仿微信评论回复(简易)相关推荐

  1. Android平台好友点击微信分享的内容后跳转来源App的实现方案研究

    很多时候我们的应用在使用微信分享内容之后,希望其他用户点击该分享内容能够跳转到我们的App,以实现闭环,这样的分享才是最有价值的.这种需求涉及到不同应用之间的交互,虽然微信提供了分享SDK,但仍然有不 ...

  2. android微信点赞ui,Android中使用PopupWindow 仿微信点赞和评论弹出

    微信朋友圈的点赞和评论功能,有2个组成部分:左下角的"更多"按钮:点击该按钮后弹出的对话框: PopupWindow,弹出框使用PopupWindow实现,这是点赞和评论的载体,具 ...

  3. android 微信评论功能,Android仿微信朋友圈点赞和评论功能

    最近在做朋友圈的项目,所以写一个Android仿朋友圈点赞和评论功能Demo,代码就是简单实现了一下功能,没有做优化,凑合看. 图文排列是用的RecyclerView实现的,弹窗效果是用的自定义的Po ...

  4. java仿qq空间音乐播放_完美实现仿QQ空间评论回复特效

    评论回复是个很常见的东西,但是各大网站实现的方式却不尽相同.大体上有两种方式 1. 像优酷这种最常见,在输入框中@要回复的人,这种方式下,用www.cppcns.com户可以修改@. 新浪微博则是在这 ...

  5. Android 仿钉钉、微信 群聊组合头像

    CombineBitmap 项目地址:SheHuan/CombineBitmap 简介: Android 仿钉钉.微信 群聊组合头像 更多:作者   提 Bug 标签: 效果预览   功能 生成类似钉 ...

  6. Android 仿钉钉、微信 群聊组合头像,Android插件化入门指南

    简介: Android 仿钉钉.微信 群聊组合头像 更多:作者   提 Bug 标签: 效果预览 | | | |   | | - | - | - | - | | | | | | | | | | | 功 ...

  7. Android录制视频,仿微信小视频录制(一)

    Android录制视频,第一部分自定义控件 简述 公司有一个录制视频并上传的功能,录制视频具体使用类如下:硬件控制使用Camera,视频录制的格式音频等具体配置与录制使用MediaRecorder,预 ...

  8. Android 仿秒拍,微信录制短视频

    Android 仿秒拍,微信录制短视频 之前看了别人写的代码,但是结果自己运行时出现了这种异常.一下是自己整理后的代码: -1.首先是自定义view :MovieRecorderView.class. ...

  9. 【凯子哥带你做高仿】“煎蛋”Android版的高仿及优化(二)——大图显示模式、评论“盖楼”效果实现详解

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在前一篇文章中,我们学习了如何进行逆向工程和TcpDump进行抓包,获取我们的数据接口,那么有了数据之后,我 ...

最新文章

  1. 安装完之后设置动态ip地址
  2. 如何备考上海市高等学校计算机一级,计算机一级考试备考攻略
  3. C#.NET操作数据库通用类
  4. 数据结构与算法-二叉树(java描述)
  5. 第二次学习笔记(linux/unix操作系统)
  6. 百度地图软件测试,使用 app-inspector 解析 i 调用百度地图定位的页面时 (iOS),会导致测试 app 崩溃...
  7. winsows10下用ninja编译配置caffe
  8. 实验四 图的实现与应用
  9. 使用Bazel编译报错ERROR: Unrecognized option: --experimental_repo_remote_exec解决方法
  10. 大数据hadoop,spark,flink等经典电子书PDF下载
  11. 管家婆辉煌 经营历程 Date exceeds maximum of 19-12-31 报错解决办法
  12. Java订单接入支付宝二 支付回调
  13. 正则表达式(以校验qq是否合法等为例)
  14. cannot be cast to com.activiti.common.config.ICustomProcessDiagramGenerator
  15. 华为数字化转型的钻石模型
  16. linux redhat下载地址
  17. 设计模式-适配器模式(类适配器、对象适配器、接口适配器详解)
  18. 史上最详细最易懂的EventBus源码解析
  19. 2021年中考计算机考试,2021年初中信息技术考试操作题
  20. 无限循环小数转化分数

热门文章

  1. Unity UI血条制作
  2. gaussian 初步 input file 的编写
  3. BCdS一电池测试系统软件安装,鑫达能电池检测设备:锂电池充放电测试设备制造商...
  4. 爬虫学习笔记---BeautifulSoup4库的使用
  5. 35个精选网站(转贴)
  6. 玩转type类型(牛逼克拉斯 )
  7. 基于java的固定资产管理系统的设计与实现
  8. 密码学 | 期末考前小记
  9. 超5k+stars,给大家推荐两个ChatGPT自动化论文阅读网站和插件,从此搞科研再也不用愁!...
  10. 大数据+未来城市交通(一)