cell里面楼层评论

之前有朋友让我写个朋友圈楼层评论的demo,本着帮着他人,提高自己的精神我就开始写了,因为能力有限,所以写的不是很好,如果有你觉得对你有用的地方可以看看,如果有不好的地方欢迎给我指出来.
先看看效果图吧.


效果差不多就是这样,我一点点分解出来,慢慢讲.....
首先先看看大体布局吧



整体是个tableview,每条信息用一个section表示,每个section2个cell,第一个cell是用户的名字,内容,时间.第二个cell里面就是回复.
首先,点击那个评论按钮弹出评论和点赞,再点一下就收回时怎么做的?首先在cell里面放一个view,view里面放评论和点赞按钮,将他们三个的width拖进代码里分别设置为:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *dianzanWidth;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *pinglunWidth;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *viewWidth;

然后给外面的评论按钮添加一个点击事件.

-(void)showMore
{
    if (_isSpread) {
        [UIView animateWithDuration:0.3 animations:^{
            [_viewWidth setConstant:80];
            [_dianzanWidth setConstant:15];
            [_pinglunWidth setConstant:15];
            [self.contentView layoutIfNeeded];
            _isSpread = NO;
        }];
    }
    else{
    [UIView animateWithDuration:0.3 animations:^{
        [_viewWidth setConstant:0];
        [_dianzanWidth setConstant:0];
        [_pinglunWidth setConstant:0];
        [self.contentView layoutIfNeeded];
        _isSpread = YES;
    }];
    }
}

给autolayout添加动画需要用到[self.contentView layoutIfNeeded];这个方法.
然后点击评论后,弹出键盘,而且键盘上面还有一个textfield,这个是用到了textfield的inputAccessoryView这个属性了.
我是这样写的:

 _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 30)];
    inputView.backgroundColor = [UIColor grayColor];
    textfield.placeholder = @"请输入评论";
    [inputView addSubview:textfield];
    textfield.delegate = self;
    [_textField setInputAccessoryView:inputView];
    _textField.delegate = self;
    [self.contentView addSubview:_textField];

点击评论按钮就让_textField成为第一响应.
再创建一个model,里面是这样的.

Model.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Model : NSObject
@property (nonatomic, strong)NSString *username;//用户名
@property (nonatomic, strong)NSString *content;//内容
@property (nonatomic, strong)NSString *time;//时间
@property (nonatomic, strong)NSMutableArray *pinglunArr;//评论数组
-(CGFloat)setHeight;//根据content设置高度
@end

Model.m

#import "Model.h"
@implementation Model
-(CGFloat)setHeight
{
    CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 88;
    CGFloat contentH = [_content boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14.0]}
                                                context:nil].size.height;
    return  100 + contentH;
}
@end

这个model就是tableview里面每个section的数据.
还有个问题就是怎么根据评论数组去显示评论呢,我是这样处理的.

-(void)setupWithArray:(NSMutableArray *)array
{
    for (UILabel *label in self.contentView.subviews) {
        [label removeFromSuperview];
    }
    if (array.count == 0) {
        return;
    }
    for (NSInteger i = 0; i < array.count; i++) {
        if (i == 0) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, [self getHeitht:array[0]])];
            [label setFont:[UIFont systemFontOfSize:12]];
            label.tag = 1000 + i;
            label.text = array[0];
            label.numberOfLines = 0;
            [self.contentView addSubview:label];
        }
        else
        {
            UILabel *lastLabel = (UILabel *)[self.contentView viewWithTag:999 + i];//获取到签名的那个label
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, lastLabel.frame.origin.y + lastLabel.frame.size.height, 300, [self getHeitht:array[i]])];
            label.tag = 1000 + i;
            label.text = array[i];
            label.numberOfLines = 0;
            [label setFont:[UIFont systemFontOfSize:12]];
            [self.contentView addSubview:label];
        }
    }
}

-(CGFloat)getHeitht:(NSString *)string
{
    CGFloat contentW = 300;
    CGFloat contentH = [string boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0]}
                                                context:nil].size.height;
    NSLog(@"%.2f",contentH);
    return contentH;

}

这个方法就是根据数组的个数去创建多少个label,然后每个label根据上面那个label的位置去摆放自己的位置.
然后再看看viewcontroller吧.

#import "ViewController.h"
#import "TestCell.h"
#import "Model.h"
#import "PinglunCell.h"
#define WEAKSELF __weak typeof(&*self) weakSelf = self;
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 7; i ++) {
        Model *model = [[Model alloc]init];
        model.username = [NSString stringWithFormat:@"徐老茂%ld",i];
        model.content = @"这是个测试的sdjfk;saljdfkl;;sajdklfjsa;ldkjf;aslkdjf;laksdjf;";
        model.time = @"1小时前";
        model.pinglunArr = [NSMutableArray array];
        [_dataArr addObject:model];
    }
    [_tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        Model *model = _dataArr[indexPath.section];
        return   [model setHeight];
    }
    else
    {
        CGFloat height = 0;
         Model *model = _dataArr[indexPath.section];
        for (NSString *str in model.pinglunArr) {
            height += [self setHeight:str];
        }
        return height;
    }

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

-(CGFloat)setHeight:(NSString *)pinglun
{
    CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 20;
    CGFloat contentH = [pinglun boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0]}
                                              context:nil].size.height;
    return  contentH + 5;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Model *model = _dataArr[section];//如果有评论就显示2个cell,没有评论就显示一个cell
    if (!model.pinglunArr.count) {
        return 1;
    }
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    if (indexPath.row == 0 ) {
        static NSString *identifier = @"testcell";
        TestCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        cell.tag = 1000 + indexPath.section;
        [cell setupWithModel:_dataArr[indexPath.section]];
        WEAKSELF
       [cell setPinglunBlock:^(Model *model, NSInteger section){
           [weakSelf.dataArr replaceObjectAtIndex:section withObject:model];
           [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
        }];
        return cell;
    }else
    {
        static NSString *identifier = @"pingluncell";
        PinglunCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        Model *model = _dataArr[indexPath.section];
        [cell setupWithArray:model.pinglunArr];
        return cell;
    }
}
@end

那么还剩一个问题,就是怎么在输入完了之后点击ruturn然后把输入的内容放到数据源呢.
我们在点击评论按钮是会获取那个cell是第几个section,因为在我应经在返回cell的代理方法里面设置了cell的tag值

 cell.tag = 1000 + indexPath.section;

我是这样做的,在testcell.h里面建一个block,

typedef void(^PinglunBlock)(Model *model,NSInteger section);

然后在点击return的方法里

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [_textField resignFirstResponder];
    NSMutableArray *arr = _model.pinglunArr;
    [arr addObject:textField.text];
    _model.pinglunArr = arr;
    self.pinglunBlock(_model,self.tag - 1000);
    return true;
}

在viewcontroller返回tableviewcell的代理方法里有

[cell setPinglunBlock:^(Model *model, NSInteger section){
           [weakSelf.dataArr replaceObjectAtIndex:section withObject:model];
           [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}];

这样就把评论的文字添加到数据源里.
好了,思路差不多就是这样,写的不是很好,以前也没遇到过这种类似的需求,所以肯定有一些很笨拙的地方,大家见谅,也希望大家能提出来.祝大家天天开心😄

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,943评论 4 60
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,306评论 0 1
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,460评论 1 14
  • 我隐约看得到,窗边的孩子在祈祷 已知的神 未知的神 从未垂青那间灯火阑珊的屋子 白炽灯,黄色的 照着那孩子焦黄的脸...
    卡斯卡阅读 210评论 0 0
  • 无论走到哪里,都应该记住,过去都是假的,回忆是一条没有尽头的路,一切过往的春天都不复存在,就连那最坚实而又最狂乱...
    辛媛梦阅读 339评论 2 4