仿微信評論回覆(簡易)
阿新 • • 發佈:2019-01-23
https://www.jianshu.com/p/0061df465650
先說說大概邏輯,一個控制器裡新增列表(類似朋友圈的每條動態),每個動態裡面再新增一個列表(用來顯示所有評論以及點選回覆評論),都是用Masonry佈局加HYBMasonryAutoCellHeight自適應行高來完成,鍵盤是隨便找的一個第三方,而且專案裡也沒重點設定
先看看大概的介面以及兩個模型裡面的屬性
下面是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;
}
}
大致效果是這樣的
程式碼寫的比較倉促,有什麼寫的不對的大家可以評論,哈哈有沒有大神願意加我個小群,自己建的,只有3個人,都比較菜,如果哪位大神平時喜歡帶新人的話加我