ChatCell

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

  }

}

时间: 2024-08-08 14:23:19

ChatCell的相关文章

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

这篇博客的代码是直接在上篇博客的基础上增加的,先给出部分代码,最后会给出能实现简单功能的完整代码. UI进阶 即时通讯之XMPP登录.注册 1.好友列表 初始化好友花名册 1 #pragma mark - 管理好友 2 // 获取管理好友的单例对象 3 XMPPRosterCoreDataStorage *rosterStorage = [XMPPRosterCoreDataStorage sharedInstance]; 4 // 用管理好友的单例对象初始化Roster花名册 5 // 好友操

即时通信

XMPPManager.h 1 #import <Foundation/Foundation.h> 2 #import "XMPPFramework.h" 3 4 @interface XMPPManager : NSObject 5 6 /** 7 * 创建一个通道的属性 8 */ 9 @property (nonatomic, strong) XMPPStream *xmppStream; 10 11 /** 12 * 管理好友,好友的列表 13 */ 14 @prop

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

1.好友列表 初始化好友花名册 1 #pragma mark - 管理好友 2 // 获取管理好友的单例对象 3 XMPPRosterCoreDataStorage *rosterStorage = [XMPPRosterCoreDataStorage sharedInstance]; 4 // 用管理好友的单例对象初始化Roster花名册 5 // 好友操作是耗时操作 6 self.xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:r

XMPP即时通讯(代码实现)

1.配置XMPP(XMPPConfig.m) 2.配置XMPPFramework框架 3.创建单例类(XMPPManager.h/XMPPManager.m)管理器 XMPPManager.m: #import "XMPPManager.h" #import "AppDelegate.h" //连接服务器的目的 typedef NS_ENUM(NSInteger, ConnectToServerPopurpose) { ConnectToServerPopurpos

《iOS 高级编程》之Tableview进阶指南

本章内容: ●    学习如何进阶使用UITableView,带给应用更高级的观感(look and feel) ●    学习如何开发自己定制的UITableView类,模仿iMessage应用的观感 ●    为一个基于分组的UITableView实现下钻逻辑 在iOS应用中呈现数据时,UITableView可能是最经常使用的用户界面对象.在本章中,将学习到以超越标准实现的方式使用UITableView,并理解UITableView类的工作方式.你会创建一个聊天视图控制器,它支持定制的单元格

Swift 2.0 : &#39;enumerate&#39; is unavailable: call the &#39;enumerate()&#39; method on the sequence

Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence 如下代码: for (index,cell) in enumerate(self.tableView.visibleCells){ if let acell = cell as? ChatCell { acell.changeColor(self.isWhiteBackground) } } 修改为如下: for (index

iOS 聊天界面

1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 7 8 @end 1 #import "AppDelegate.h" 2 #import "YXYCViewController.h" 3 @interfa