有的软件有评论功能,有评论就有回复,所以回复是怎么做的呐?
首先看效果
首定义一个tableview
上面是普通的文本和按钮,但是按钮要加一个属性,便于区分哪一行,所以我继承了UIButton重写了一个按钮
@interface UIIndexButton : UIButton
@property(strong,nonatomic)NSIndexPath *indexPath;
@end
然后在这个cell上自定义一个代理
定义代理
@protocol CommentCellDelegate <NSObject>
-(void)CommentCellDelegateWithIndexPath:(NSIndexPath *)indexPath;
@end
@interface CommentCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *commentLabel;
@property (weak, nonatomic) IBOutlet UIIndexButton *commentBtn;
@property(weak,nonatomic)id<CommentCellDelegate>delegate;
-(void)configWithModel:(CommentModel *)model;
@end
实现代理
- (IBAction)commentAction:(UIIndexButton *)sender {
[self.delegate CommentCellDelegateWithIndexPath:sender.indexPath];
}
-(void)configWithModel:(CommentModel *)model
{
self.commentLabel.text = model.commentString;
}
最后在tableview的代理方法中实现代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
[cell configWithModel:self.dataArr[indexPath.row]];
cell.commentBtn.indexPath = indexPath;
cell.delegate = self;
return cell;
}
#pragma mark - comment
-(void)CommentCellDelegateWithIndexPath:(NSIndexPath *)indexPath
{
//找到点击的单元格 取出该单元格的对象
CommentCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CommentModel *model = self.dataArr[indexPath.row];
CommentModel *commentModel = [[CommentModel alloc] init];
commentModel.commentString = [NSString stringWithFormat:@"@某某%@---评论测试",model.commentString];
// commentModel.commentString = @"留下唇印的嘴";
//插入新数据源 刷新当前页面 post请求
[self.dataArr insertObject:commentModel atIndex:0];
[self.tableView reloadData];
NSLog(@"=====%@====%ld====%@",model.commentString,indexPath.row,cell.commentLabel.text);
}
这样,一个粗糙的回复功能就有了