iOS评论回复功能实现

此方法较为繁琐,博主已经写了一篇新文章,这种方式用户体验更好一些。

链接:http://www.jianshu.com/p/a120dc88899e

核心代码:

// 点击评论回复按钮

-(void)reply:(UIButton *)button{

if (button.selected == NO) {// 点击回复时

NSDictionary *dic =@{};

NSDetailCommentCellModel *model = [[NSDetailCommentCellModel alloc]initWithDictionary:dic error:nil];

[_dataArray insertObject:model atIndex:button.tag+1];

[self replyInsertTableviewRow:button.tag +1];

[_longDetailView.tableview reloadData];

if (button.tag<_markIdex) {

_markIdex = _markIdex+1;

}

if (_markIdex > 0) {

[_dataArray removeObjectAtIndex:_markIdex];

[self replyDeleteTableviewRow:_markIdex];

[_longDetailView.tableview reloadData];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_markIdex-1 inSection:0];

NSHotCommentCell *cell = [_longDetailView.tableview cellForRowAtIndexPath:indexPath];

if (cell == nil) {

HotCommentReplyCell *cell = [_longDetailView.tableview cellForRowAtIndexPath:indexPath];

[cell.replyBtn setSelected:NO];

[cell.replyBtn setTitle:@"回复" forState:UIControlStateNormal];

}else{

[cell.replyBtn setSelected:NO];

[cell.replyBtn setTitle:@"回复" forState:UIControlStateNormal];

}

}

_markIdex = button.tag+1;

[button setSelected:YES];

[button setTitle:@"取消" forState:UIControlStateNormal];

//更新布局

[_longDetailView.tableview mas_updateConstraints:^(MASConstraintMaker *make) {

make.height.equalTo(@(_longDetailView.tableview.contentSize.height+64));

}];

[_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {

make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);

make.bottom.mas_greaterThanOrEqualTo(_longDetailView);

}];

}else{// 点击取消时

[_dataArray removeObjectAtIndex:button.tag+1];

[self replyDeleteTableviewRow:button.tag+1];

[_longDetailView.tableview reloadData];

[button setSelected:NO];

[button setTitle:@"回复" forState:UIControlStateNormal];

_markIdex = -1;

//更新布局

[_longDetailView.tableview mas_updateConstraints:^(MASConstraintMaker *make) {

make.height.equalTo(@(_longDetailView.tableview.contentSize.height+64));

}];

[_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {

make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);

make.bottom.mas_greaterThanOrEqualTo(_longDetailView);

}];

}

}

// 回复插入一行

- (void)replyInsertTableviewRow:(NSInteger)index{

[_longDetailView.tableview beginUpdates];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];

[_longDetailView.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

[_longDetailView.tableview endUpdates];

[_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {

make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);

make.bottom.mas_greaterThanOrEqualTo(_longDetailView);

}];

}

// 回复删除一行

- (void)replyDeleteTableviewRow:(NSInteger)index{

[_longDetailView.tableview beginUpdates];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];

[_longDetailView.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

[_longDetailView.tableview endUpdates];

[_longDetailView.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {

make.bottom.mas_equalTo(_longDetailView.tableview.mas_bottom);

make.bottom.mas_greaterThanOrEqualTo(_longDetailView);

}];

}


推荐阅读更多精彩内容