同cell实现qq聊天框效果

作者: letaibai | 来源:发表于2016-04-20 12:29 被阅读181次

    qq聊天框是一个非常典型的cell.这个项目既可以用同一个cell来完成,也可以分成两个cell来操作.

    ** 准备工作: **

    • 新建DLmessage模型类文件.
    • 新建DLMessageCell类文件,将storyboard中cell的class改为DLMessageCell.设置cell的重用标识为message
    • 新建DLMessageViewController类文件,将storyboard中的控制器class改为DLMessageViewControlle.

    1.将所需控件布局到storyboard,并设置控件的图片等属性,并设置控制器为tableView的数据源和代理
    如下图:

    设置控件 设置代理和数据源

    2.通过autoLayout设置每个控件的布局,其中button的宽度属性大于等于 60 小于等于 240 ,这样button里面的titleLabel属性就能跟随内容长度变化,从而也就决定了button的宽度长短.在自定义cell类的awakeFromNib方法中设置button中titleLabel的numberOfLines为零,这样就实现了Button内容的自动换行功能.

    - (void)awakeFromNib{
       //设置titleLabel达到button最大宽度时自动换行
        self.messageButton.titleLabel.numberOfLines = 0;
        self.otherButtonLable.titleLabel.numberOfLines=0;
    }
    

    3.将控件连线DLMessageCell类扩展中.

    连线

    4.创建DLmessage模型中的成员属性,使其能够加载并记录plist文件数据.

    DLmessage.h文件
    #import <UIKit/UIKit.h>
    
    typedef enum {
        DLMessageTypeMe = 0,
        DLMessageTypeOther = 1
    } DLMessageType;
    
    @interface DLMessage : NSObject
    @property(nonatomic,strong) NSString *text;
    @property(nonatomic,strong) NSString *time;
    @property(nonatomic,assign) DLMessageType type;
    //记录行高,取出cell中高度的最大值来赋值
    @property(nonatomic,assign) CGFloat cellHeight;
    //记录发送时间是否显示
    @property(nonatomic,assign,getter=isHideTime) BOOL hideTime;
    //构造方法,方便外界传入plist文件
    + (instancetype)messageWithDict:(NSDictionary *)dict;
    @end
    
    DLmessage.m文件
    #import "DLMessage.h"
    
    @implementation DLMessage
    + (instancetype)messageWithDict:(NSDictionary *)dict{
        DLMessage *message = [[self alloc] init];
        //KVC
        [message setValuesForKeysWithDictionary:dict];
        return message;
    }
    @end
    

    5.在DLMessageCell.h文件中新建模型类成员属性,在.m中重写set方法,将数据传给子控件

    DLMessageCell.h文件
    
    #import <UIKit/UIKit.h>
    
    @class DLMessage;
    
    @interface DLMessageCell : UITableViewCell
    
    @property(nonatomic,strong) DLMessage *message;
    @end
    
    DLMessageCell.m文件
    
    #import "DLMessageViewCell.h"
    #import "DLMessage.h"
    //define this constant if you want to use Masonry without the 'mas_' prefix
    #define MAS_SHORTHAND
    //define this constant if you want to enable auto-boxing for default syntax
    #define MAS_SHORTHAND_GLOBALS
    #import "Masonry.h"
    
    @interface MessageViewCell ()
    
     /**  获得成员变量  */
    @property (weak, nonatomic) IBOutlet UIImageView *nameView;
    @property (weak, nonatomic) IBOutlet UIButton *ButtonLabel;
    @property (weak, nonatomic) IBOutlet UILabel *timeLabel;
    @property (weak, nonatomic) IBOutlet UIButton *otherButtonLabel;
    @property (weak, nonatomic) IBOutlet UIImageView *otherNameView;
    
    @end
    
    @implementation DLMessageViewCell
    
     /**  设置自动换行  */
    -(void)awakeFromNib
    {
        self.ButtonLable.titleLabel.numberOfLines=0;
        self.otherButtonLable.titleLabel.numberOfLines=0;
    }
     /**  设置模型的值给成员变量 */
    -(void)setMessage:(Message *)message
    {
        _message=message;
        
        if (message.hideTime) { // 隐藏时间
            self.timeLabel.hidden = YES;
            [self.timeLabel updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(0);
            }];
        } else { // 显示时间
            self.timeLabel.text = message.time;
            self.timeLabel.hidden = NO;
            [self.timeLabel updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(21);
            }];
        }
        //根据模型的type设置显示哪一边的cell,并设置内容
        if (message.type == DLMessageTypeMe) {
            [self settingShowButton:self.ButtonLabel ShowImageView:self.nameView HideButton:self.otherButtonLabel HideImageView:self.otherNameView];
        }else{
            [self settingShowButton:self.otherButtonLabel ShowImageView:self.otherNameView HideButton:self.ButtonLabel HideImageView:self.nameView];
        }
    
    }
      //方法抽出
    - (void)settingShowButton:(UIButton *)showButton ShowImageView:(UIImageView *)showImageView HideButton:(UIButton *)hideButton HideImageView:(UIImageView *)hideImageView{
        
        hideButton.hidden=YES;
        hideImageView.hidden=YES;
        
        showImageView.hidden=NO;
        showButton.hidden=NO;
        
        [showButton setTitle:self.message.text forState:UIControlStateNormal];
        //强制更新,确保数值更新
        [showButton layoutIfNeeded];
        //设置button的高度与label相同
        [showButton updateConstraints:^(MASConstraintMaker *make) {
            CGFloat ButtonH=showButton.titleLabel.frame.size.height + 30;
            make.height.equalTo(ButtonH);
        }];
        //强制更新,确保数值更新
        [showButton layoutIfNeeded];
        //获取button与nameView的最大值,设置最大值为cell的高度
        CGFloat buttonMaxY = CGRectGetMaxY(showButton.frame) + 10;
        CGFloat iconMaxY = CGRectGetMaxY(showImageView.frame) + 10;
        self.message.cellHeight = MAX(buttonMaxY, iconMaxY);    
    }
    @end
    

    6.在DLMessageViewController中新建数组,加载模型数据,同时实现tableView的数据源方法,需遵守UITableViewDataSource,UITableViewDelegate协议.

    DLMessageViewController.m文件
    
    #import "DLMessageViewController.h"
    #import "DLMessageViewCell.h"
    #import "DLMessage.h"
    
    @interface DLMessageViewController ()<UITableViewDataSource,UITableViewDelegate>
    
    @property(nonatomic,strong) NSArray *messages;
    
    @end
    
    @implementation MessageViewController
    
    #pragma mark - 懒加载
    -(NSArray *)messages
    {
        if (_messages==nil) {
            NSString *path=[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];
            NSArray *dictArray=[NSArray arrayWithContentsOfFile:path];
            NSMutableArray *messageArr=[NSMutableArray array];
            //定义一个模型接收上一次的时间
            Message *lastMessage = nil;
            for (NSDictionary *dict in dictArray) {
                Message *message=[Message messageWithDict:dict];
                //比较两次的时间是否相同,返回BOOL值
                message.hideTime = [lastMessage.time isEqualToString:message.time];
                [messageArr addObject:message];
                记录上一个遍历出来的模型
                lastMessage = message;
            }
            _messages=messageArr;
        }
        return _messages;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    #pragma mark - 设置cell
    
     /**  设置cell数  */
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.messages.count;
    }
     /**  设置每行cell显示的内容  */
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MessageViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"message"];
        cell.message = self.messages[indexPath.row];
        return cell;
    }
     /**  cell的高度*/
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Message *message=self.messages[indexPath.row];
        return message.cellHeight;
    }
     /**  设置cell的虚拟行高  */
    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 200;
    }
    @end
    

    相关文章

      网友评论

        本文标题:同cell实现qq聊天框效果

        本文链接:https://www.haomeiwen.com/subject/epbzlttx.html