仿微信評(píng)論回復(fù)(簡易)
?
2016.12.16 19:24*?字?jǐn)?shù) 283?閱讀 976評(píng)論 17喜歡 5
簡單的微信評(píng)論回復(fù)功能
先說說大概邏輯,一個(gè)控制器里添加列表(類似朋友圈的每條動(dòng)態(tài)),每個(gè)動(dòng)態(tài)里面再添加一個(gè)列表(用來顯示所有評(píng)論以及點(diǎn)擊回復(fù)評(píng)論),都是用Masonry布局加HYBMasonryAutoCellHeight自適應(yīng)行高來完成,鍵盤是隨便找的一個(gè)第三方,而且項(xiàng)目里也沒重點(diǎn)設(shè)置
先看看大概的界面以及兩個(gè)模型里面的屬性

1.png

2.png

3.png
下面是viewController里面代碼,這里把回復(fù)的人的名字定死了,有數(shù)據(jù)的話可以根據(jù)實(shí)際情況來定
#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@interfaceViewController()@property(nonatomic,strong)UITableView*tableview;@property(nonatomic,strong)NSArray*allMessage;@property(nonatomic,strong) ChatKeyBoard *chatKeyBoard;@property(nonatomic,strong)NSIndexPath*myIndexPath;//回復(fù)給誰@property(nonatomic,strong)NSString*name;@end@implementationViewController-(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 =@"評(píng)論";? ? ? ? [self.view addSubview:_chatKeyBoard];? ? ? ? [self.view bringSubviewToFront:_chatKeyBoard];? ? }return_chatKeyBoard;}- (void)viewDidLoad {? ? [superviewDidLoad];self.view.backgroundColor = [UIColorwhiteColor];self.tableview = [[UITableViewalloc]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 =@"真羨慕你們這么年紀(jì)輕輕就認(rèn)識(shí)像我這么有才華的人";? ? 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 {returnself.allMessage.count;}- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {? ? TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];if(!cell) {? ? ? ? cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];? ? ? ? cell.delegate =self;? ? }? ? [cell configCellWithModel:self.allMessage[indexPath.row] indexPath:indexPath];returncell;}- (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 = [NSStringstringWithFormat:@"回復(fù)%@:",model.remarkPeople];? ? }else{self.chatKeyBoard.placeHolder = [NSStringstringWithFormat:@"回復(fù)%@:",model.startPeople];? ? }self.name = model.startPeople;? ? [self.chatKeyBoard keyboardUpforComment];self.myIndexPath = indexPath;}//發(fā)送- (void)chatKeyBoardSendText:(NSString*)text{? ? [self.chatKeyBoard keyboardDownForComment];? ? ? ? AllComentModel *model =self.allMessage[self.myIndexPath.row];//評(píng)論人model,評(píng)論人名寫死了ComentModel *commodel = [ComentModel new];? ? commodel.startPeople =self.name;? ? commodel.remarkText = text;? ? commodel.remarkPeople =@"馬化騰";NSMutableArray*mtAry = [NSMutableArrayarrayWithArray:model.allComents];? ? [mtAry addObject:commodel];? ? model.allComents = mtAry.mutableCopy;? ? ? ? [self.tableview reloadRowsAtIndexPaths:@[self.myIndexPath] withRowAnimation:UITableViewRowAnimationFade];}
下面是第一個(gè)tableViewCell的.h和.m
#import#import"ComentModel.h"#import"AllComentModel.h"@classTableViewCell;@protocolTableviewCellDelegate- (void)clickCellWithModel:(ComentModel *)model andIndex:(NSIndexPath*)indexPath;@end@interfaceTableViewCell: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@interfaceTableViewCell()@property(nonatomic,strong)NSIndexPath*indexPath;@property(nonatomic,strong) AllComentModel *allComents;@end@implementationTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {if(self= [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {self.backgroundColor = [UIColorlightGrayColor];self.tableView = [[UITableViewalloc] 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;? ? }returnself;}- (void)configCellWithModel:(AllComentModel *)model indexPath:(NSIndexPath*)indexPath {CGFloattableviewHeight =0;self.allComents = model;self.indexPath = indexPath;for(ComentModel *comentModelinmodel.allComents) {CGFloatcellheight = [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 {returnself.allComents.allComents.count;}- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {? ? MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];if(!cell) {? ? ? ? cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"Cell"];? ? }? ? ComentModel *model =self.allComents.allComents[indexPath.row];? ? [cell configCellWithModel:model];returncell;}- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {? ? ComentModel *model =self.allComents.allComents[indexPath.row];CGFloatcell_height = [MessageCell hyb_heightForTableView:tableView config:^(UITableViewCell*sourceCell) {? ? ? ? MessageCell *cell = (MessageCell *)sourceCell;? ? ? ? [cell configCellWithModel:model];? ? } cache:^NSDictionary*{NSDictionary*cache = @{kHYBCacheUniqueKey :@"",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? kHYBCacheStateKey :@"",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? kHYBRecalculateForStateKey : @(YES)};returncache;? ? }];returncell_height;}- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {//當(dāng)點(diǎn)擊到自己回復(fù)的語句時(shí),微信是彈出刪除功能,需要的也可以自己加上去ComentModel *model =self.allComents.allComents[indexPath.row];if([model.remarkPeople isEqualToString:@"馬化騰"]) {NSLog(@"點(diǎn)擊了自己的回復(fù)");return;? ? }if([self.delegate respondsToSelector:@selector(clickCellWithModel:andIndex:)]) {? ? ? ? [self.delegate clickCellWithModel:model andIndex:self.indexPath];? ? }}
下面是主列表里面包含的評(píng)論列表的.h和.m
#import#import"ComentModel.h"@interfaceMessageCell:UITableViewCell@property(nonatomic,strong)UILabel*contentLabel;- (void)configCellWithModel:(ComentModel *)model;@end
/**如果想做類似微信那樣點(diǎn)擊評(píng)論人名和回復(fù)人名跳轉(zhuǎn)的話可以在這里寫,我這里總的寫成了一個(gè)label*/#import"MessageCell.h"#import"Masonry.h"#import"UITableViewCell+HYBMasonryAutoCellHeight.h"@implementationMessageCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {if(self== [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {// contentLabelself.contentLabel = [[UILabelalloc] init];? ? ? ? [self.contentView addSubview:self.contentLabel];self.contentLabel.backgroundColor = [UIColorclearColor];self.contentLabel.preferredMaxLayoutWidth = [UIScreenmainScreen].bounds.size.width -80;self.contentLabel.numberOfLines =0;self.contentLabel.font = [UIFontsystemFontOfSize: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;? ? }returnself;}- (void)configCellWithModel:(ComentModel *)model {NSString*str =nil;if(![model.remarkPeople isEqualToString:@""]) {? ? ? ? str = [NSStringstringWithFormat:@"%@ 回復(fù) %@: %@",model.remarkPeople,model.startPeople,model.remarkText];NSMutableAttributedString*text = [[NSMutableAttributedStringalloc] initWithString:str];? ? ? ? [text addAttribute:NSForegroundColorAttributeNamevalue:[UIColororangeColor]? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, model.remarkPeople.length)];? ? ? ? [text addAttribute:NSForegroundColorAttributeNamevalue:[UIColororangeColor]? ? ? ? ? ? ? ? ? ? range:NSMakeRange(model.remarkPeople.length +4, model.startPeople.length+1)];self.contentLabel.attributedText = text;? ? }else{? ? ? ? str = [NSStringstringWithFormat:@"%@: %@",model.startPeople,model.remarkText];NSMutableAttributedString*text = [[NSMutableAttributedStringalloc] initWithString:str];? ? ? ? [text addAttribute:NSForegroundColorAttributeNamevalue:[UIColororangeColor]? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, model.startPeople.length+1)];self.contentLabel.attributedText = text;? ? }}
大致效果是這樣的

xxxx.gif
代碼寫的比較倉促,有什么寫的不對(duì)的大家可以評(píng)論,哈哈
有沒有大神愿意加我個(gè)小群,自己建的,只有3個(gè)人,都比較菜,如果哪位大神平時(shí)喜歡帶新人的話加我群:515385179 哈哈