iOS实现搭建聊天页面的实例代码

2020-01-20 23:56:01王旭

输入框 UITableViewHeaderFooterView


//
// WDPersonMessageFooterCell.h
// WestDevelopment
//
// Created by wangtao on 2017/6/26.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDBaseTVHeaderFooterView.h"

typedef void(^ClickSender_t)(NSString *text);

typedef void(^ResignFirstResponder)();

@interface WDPersonMessageFooterCell : WDBaseTVHeaderFooterView

@property (nonatomic, copy) ClickSender_t clickSenderText;
@property (nonatomic, copy) ResignFirstResponder resignFirstRes;

@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, assign) BOOL sendSucceed;

@end


//
// WDPersonMessageFooterCell.m
// WestDevelopment
//
// Created by wangtao on 2017/6/26.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageFooterCell.h"


@interface WDPersonMessageFooterCell () <UITextFieldDelegate>

@property (nonatomic, weak) UITextField *textField;
@property (nonatomic, weak) UIView *line;

@end


@implementation WDPersonMessageFooterCell

@synthesize isFirst = _isFirst;

- (void)setupAll
{
  self.contentView.backgroundColor = WTHexColor(0xf2f2f2);

  UITextField *textField = [[UITextField alloc] init];
  textField.backgroundColor = kWhiteColor;
  [self.contentView addSubview:textField];
  textField.delegate = self;
  self.textField = textField;
  textField.layer.cornerRadius = 3;
  textField.layer.masksToBounds = YES;

  textField.returnKeyType = UIReturnKeySend;

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageState:) name:kMessageState object:nil];

  UIView *line = [[UIView alloc] init];
  line.backgroundColor = WTHexColor(0xdddddd);
  [self.contentView addSubview:line];
  self.line = line;

}

- (void)messageState:(NSNotification *)noti
{
  NSInteger state = [[noti object] boolValue];
  if (state) {
    [self.textField resignFirstResponder];
    if (self.resignFirstRes) {
      self.resignFirstRes();
    }
  }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  if (self.clickSenderText) {
    self.clickSenderText(self.textField.text);
  }
  return YES;
}

- (void)setIsFirst:(BOOL)isFirst
{
  _isFirst = isFirst;
  if (isFirst) {
    [self.textField becomeFirstResponder];
  } else {
    [self.textField resignFirstResponder];
  }
}

- (BOOL)isFirst
{
  if ([self.textField isFirstResponder]) {
    return YES;
  }
  return NO;
}

- (void)setSendSucceed:(BOOL)sendSucceed
{
  self.textField.text = @"";
}

- (void)layoutSubviews
{
  [super layoutSubviews];

  CGFloat padding = 10;
  self.textField.frame = CGRectMake(padding, padding, self.wt_width - padding * 2, self.wt_height - padding * 2);
  self.line.frame = CGRectMake(0, 0, self.wt_width, .5);
}

@end

消息cell


//
// WDPersonMessageDetailCell.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailCell.h"
#import "WDPersonMessageDetailModel.h"


@interface WDPersonMessageDetailCell ()

@property (nonatomic, weak) UILabel *time;
@property (nonatomic, weak) UIImageView *icon;
@property (nonatomic, weak) UILabel *detail;

@property (nonatomic, weak) UIView *baseView;

@end

@implementation WDPersonMessageDetailCell

- (void)setupAll
{
  self.selectionStyle = UITableViewCellSelectionStyleNone;

  self.backgroundColor = WTHexColor(0xeaeaea);
  self.contentView.backgroundColor = WTHexColor(0xeaeaea);

  UILabel *time = [UILabel labelWithText:@""
                 textColor:WTHexColor(0xaaaaaa)
               textAlignment:NSTextAlignmentCenter
                   font:12
              backgroundColor:kClearColor];
  [self.contentView addSubview:time];
  self.time = time;

  UIImageView *icon = [[UIImageView alloc] init];
  [self.contentView addSubview:icon];
  icon.image = [UIImage imageNamed:kDefault];
  self.icon = icon;
  self.icon.layer.cornerRadius = 35 / 2;
  self.icon.layer.masksToBounds = YES;

  UIView *baseView = [[UIView alloc] init];
  [self.contentView addSubview:baseView];
  self.baseView = baseView;
  baseView.layer.masksToBounds = YES;
  baseView.layer.cornerRadius = 4;

  UILabel *detail = [UILabel labelWithText:@""
                  textColor:kBlackColor
                textAlignment:NSTextAlignmentLeft
                    font:13
               backgroundColor:kClearColor];
  [baseView addSubview:detail];
  self.detail = detail;
  detail.numberOfLines = 0;

}

- (void)setModel:(WDPersonMessageDetailModel *)model
{
  _model = model;

  if ([model.isShow isEqualToString:@"1"]) {
    self.time.text = model.addTime;
    self.time.hidden = NO;
    self.time.frame = CGRectMake(0, 0, kMainScreenWidth, 20);

  } else {
    self.time.text = @"";
    self.time.hidden = YES;
    self.time.frame = CGRectZero;

  }

  self.time.text = model.addTime;
  [self.icon wt_setImageWithUrlString:model.headImg placeholderString:@"me_icon"];
  self.detail.text = model.comment;

  if ([model.userId isEqualToString:[WTAccount shareAccount].uid]) {
    self.detail.textColor = kBlackColor;
    self.baseView.backgroundColor = kWhiteColor;

    self.icon.frame = CGRectMake(kPadding, self.time.wt_bottom + kPadding, 35, 35);
    self.baseView.frame = CGRectMake(self.icon.wt_right + kPadding, self.icon.wt_top, model.commentW, model.commentH);
    self.detail.frame = CGRectMake(kPadding, kPadding, model.commentW - kPadding * 2, model.commentH - kPadding * 2);

  } else {
    self.detail.textColor = kWhiteColor;
    self.baseView.backgroundColor = kHomeColor;

    self.icon.frame = CGRectMake(kMainScreenWidth - 35 - kPadding, self.time.wt_bottom + kPadding, 35, 35);
    self.baseView.frame = CGRectMake(self.icon.wt_left - kPadding - model.commentW, self.icon.wt_top, model.commentW, model.commentH);
    self.detail.frame = CGRectMake(kPadding, kPadding, model.commentW - kPadding * 2, model.commentH - kPadding * 2);

  }

}


@end