import UIKit

let chatCellHeight: CGFloat = 72

let chatCellInsetLeft =  chatCellHeight + 8

class ChatCell:UITableViewCell {

  let userPictureImageView:UIImageView

  let userNameLabel:UILabel

  let lastMessageTextLabel:UILabel

  let lastMessageSentDateLable:UILabel

  let userNameInitialsLabel:UILabel

  override init(style:UITableViewCellStyle, reuseIdentifier:String?) {

    let pictureSize:CGFloat = 64

    userPictureImageView = UIImageView(frame:CGRect(x:8,y:(chatCellHeight-pictureSize)/2,width:pictureSize,height:pictureSize))

    userPictureImageView.backgroundColor = UIColor(red:199/255.0,green:199/255.0,blue:204/255.0,alpha:1)

    userPictureImageView.layer.cornerRadius = pictureSize/2

    userPictureImageView.layer.masksToBounds = true//将位于它之下的层

    userNameLabel = UILabel(frame:CGRectZero)

    userNameLabel.backgroundColor = UIColor.whiteColor

    userNameLabel.font = UIFont.systemFontOfSize(17)

    

    lastMessageTextLabel = UILabel(frame:CGRectZero)

    lastMessageTextLabel.backgroundColor = UIColor.whiteColor()

    lastMessageTextLabel.font = UIFont.systemFontOfSize(15)

    lastMessageTextLabel.numberOfLines= 2

    lastMessageTextLabel.textColor = UIColor(red:142/255,green:142/255,blue:147/255,alpha:1)

    

    lastMessageSentDateLabel = UILabel(frame:CGRectZero)

    lastMessageSentDateLabel.autoresizingMask = .FlexibleLeftMargin

    lastMessageSentDateLabel.backgroundColor = UIColor.whiteColor()

    lastMessageSentDateLabel.font = UIFont.systemFontOfSize(15)

    lastMessageSentDateLabel.textColor = lastMessageTextLabel.textColor

    userNameInitialsLabel = UILabel(frame:CGRectZero)

    userNameInitialsLabel.font = UIFont.systemFontOfSize(33)

    userNameInitialsLabel.textAlignment = .Center

    userNameInitialsLabel.textColor = UIColor.whiteColor()

    

    super.init(style:style, reuseIdentifier:reuseIdentifier)

    contentView.addSubView(userPictureImageView)

    contentView.addSubView(userNameLabel)

    contentView.addSubView(lastMessageTextLabel)

    contentView.addSubView(lastMessageSentDataLabel)

    userPictureImageView.addSubview(userNameInitialsLabel)

    

    userNameLabel.setTranslateAutoresizingMaskIntoConstraints(false)

    contentView.addConstraint(NSLayoutConstraint(item:userNameLabel,attribute:.Left,.Equal, toItem:contentView,attribute:.Left,multipler:1,constant:chatCellInsetLeft))

    contentView.addConstraint(NSlayoutContrstraint(item: userNameLabel,attribute:.Top,

    relatedBy:.Equal,toItem:contentView,attribute:.Top,multiplier:1,constant:6))

    lastMessageTextLabel.setTranslatesAutoresizingMaskIntoConstraints(true)

    super.init(style:style, reuseIdentifier:reuseIdentifier)

    contentView.

    lastMessageTextLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

    contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.Left,relatedBy:.Equal,toItem:userNameLabel,attribute:.Left,mltipllier:1,constrant:0))

    contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.Top,relatedBy:.Equal,toItem:contentView.,attribute:.Top,multiplier:1,constant:28))

contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.right,relatedBy:.Equal,toItem:contentView,attribute:.Right,multiplier:1,constant:-7))

contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.Bottom,relatedBy:.LessThanOrEquqal,toItem:contentView,attribute:.Bottom,multiplier:1,constant:-4))

lastMessageSentDateLabel.setTranslateAutoresizingMaskIntoConstrains(false)

contentView.addConstraint(NSLayoutConstraint(item:lastMessageSentDateLabel,attribute:.Left,elatedBy:.Equal,toItem:userNameLable,attribute:.Right,multiplierL1,constant:2))

contentView.addConstraint(NSLayoutConstraint(item:lastMessageSentDateLabel,attribute:.Right,relatedBy:.Equal,toItem:contentView,attribute:.Right,multipliter:1,constant:-7))

contentView.addConstraint(NSlabyoutConstraint(item:lastMessageSentDateLabel,attribute:.Baseline,relaedBy:.Equal,toItem:userNameLable,attrubute:.Baseline,multiplier:1,constant:0))

userNameInitialsLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

userPictureImageView.addConstraint(NSLayoutConstraint(item:userNameInitialsLabel,attribute:.CenterX,relatedBy:.Equal,toItem:userPictureImageView,attribute:.CenterX,multiplier:1,constant:0))

userPictureImageView.addConstraint(NSLayoutConstraint(item:userNameInitialsLabel,attribue:.CenterY,relatedBy:.Equal,toItem:userPictureImageView,attribute:.CenterY,multiplier:1,constant:-1))

  }

  required init(coder aDecoder:NSCoder) {

    fatalError("init(coder:) has not been implemented")

  }

  func configureWithChat(chat:Chat) {

    let user = chat.user

    userPictureImageView.image = UIImage(named:user.pictureName())

    if userPictureImageView.image == nil {

      let initials = user.initials

      if initials != nil {

        userNameInitialsLabel.text = initials

        userNameInitialsLabel.hidden = false

      } else {

        userPictureImageView.image = UIImage(named:"User0")

        userNameInitialsLabel.hidden = true

      }

    } else {

      userNameInitialsLabel.hidden = true

    }

    userNameLabel.text = user.name

    lastMessageTextLabel.text = chat.lastMessageText

    lastMessageSentDateLabel.text = chat.lastMessageSentDateString

  }

}

ChatCell的更多相关文章

  1. 即时通讯UI-聊天界面(UITableView显示左右两人聊天)

    目录 1.创建UITableView对象并设置相关属性 2.创建cellModel模型 //枚举类型 typedef enum { ChatMessageFrom = ,//来自对方的消息 ChatM ...

  2. Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence

    Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence 如下代码: for (ind ...

  3. XMPP即时通讯(代码实现)

    1.配置XMPP(XMPPConfig.m) 2.配置XMPPFramework框架 3.创建单例类(XMPPManager.h/XMPPManager.m)管理器 XMPPManager.m: #i ...

  4. UI进阶 即时通讯之XMPP好友列表、添加好友、获取会话内容、简单聊天

    这篇博客的代码是直接在上篇博客的基础上增加的,先给出部分代码,最后会给出能实现简单功能的完整代码. UI进阶 即时通讯之XMPP登录.注册 1.好友列表 初始化好友花名册 #pragma mark - ...

  5. iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 整体布局如下:     程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang  ...

  6. IOS XMPP总结

    //前言:仿weixin实现一个即时通讯的案例,支持版本7.0以上 "准备工作 创建项目时使用git" /* 显示隐藏文件,看到git的文件夹 defaults write com ...

  7. iOS 聊天界面

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  8. iOS实现XMPP通讯(二)XMPP编程

    项目概述 这是一个可以登录jabber账号,获取好友列表,并且能与好友进行聊天的项目. 使用的是第三方库XMPPFramework框架来实现XMPP通讯. 项目地址:XMPP-Project 项目准备 ...

随机推荐

  1. 《Web 前端面试指南》2、JavaScript 的 Bind 函数进阶

    使用 Bind() 设置方法中 this 对象 //<button>获取随机的人</button>​ //<input type="text"> ...

  2. mongoDB的基本使用----飞天博客

    Mongo的介绍:这个mongoDB官网说的好啊,MongoDB是一个开源的基于document的数据库,并且是优秀的NoSQL数据库,并且它是用C++写滴哈,非常有效率.一些什么特点呢? 全索引支持 ...

  3. RT: TCP connection close

    CLOSE is an operation meaning "I have no more data to send." The notion of closing a full- ...

  4. Java基础知识强化之集合框架笔记07:Collection集合的遍历之迭代器遍历

    1. Collection的迭代器: Iterator iterator():迭代器,集合的专用遍历方式 2. 代码示例: package cn.itcast_03; import java.util ...

  5. CentOS下FTP服务器安装与配置

    安装vsftpd yum install vsftpd 启动/重启/关闭vsftpd服务器 CentOS7 以下: 启动: service vsftpd start 停止: service vsftp ...

  6. new Date()在IE,谷歌,火狐上的一些注意项

    1.new Date()在IE浏览器上IE9以上的可以直接使用new Date("yyyy-MM-dd"),但是在IE8上的时候就要使用new Date("yyyy/MM ...

  7. 状态开关按钮(ToggleButton)及按钮(Swich)的使用

    状态开关按钮(ToggleButton)和开关(Switch)也是由Button派生出来的,因此它们本质上都是按钮,Button支持的各种属性.方法也适用于ToggleButton和Switch.从功 ...

  8. 创建dblink遇到一系列问题

    创建dblink遇到一系列问题,有时间 把问题整理一下

  9. react服务端渲染(同构)

    学习react也有一段时间了,使用react后首页渲染的速度与seo一直不理想.打算研究一下react神奇服务端渲染. react服务端渲染只能使用nodejs做服务端语言实现前后端同构,在后台对re ...

  10. Hibernate 性能优化之查询缓存

    查询缓存是建立在二级缓存基础之上的,所以与二级缓存特性相似,是共享的,适合修改不是很频繁的数据 查询缓存不是默认开启的,需要设置      1.在cfg文件中配置 <property name= ...