项目以及封装iOS DeveloperiOS学习笔记

评论界面的回复是怎么做的猜想。

2017-02-08  本文已影响92人  qazws

有的软件有评论功能,有评论就有回复,所以回复是怎么做的呐?

首先看效果


Simulator Screen Shot 2017年2月8日 下午1.29.58.png Simulator Screen Shot 2017年2月8日 下午1.30.02.png

首定义一个tableview

上面是普通的文本和按钮,但是按钮要加一个属性,便于区分哪一行,所以我继承了UIButton重写了一个按钮

@interface UIIndexButton : UIButton
@property(strong,nonatomic)NSIndexPath *indexPath;
@end
屏幕快照 2017-02-08 下午1.33.29.png

然后在这个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);
}


这样,一个粗糙的回复功能就有了

代码传送门

上一篇 下一篇

猜你喜欢

热点阅读