我需要使用JSQMessagesViewController在单元格内创建表单。我怎样才能做到这一点?我正在尝试实现自定义单元格。
我可以使用多个不同的自定义单元格类吗?如果是,那怎么样? 这是我能够做到的 -
class ViewController: JSQMessagesViewController {
//MARK: Properties
var messages = [JSQMessage]()
var outgoingBubbleImageView: JSQMessagesBubbleImage!
var incomingBubbleImageView: JSQMessagesBubbleImage!
//MARK: Input button methods
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
print("sending message '\(text)'")
self.addMessage("2", text: text)
self.inputToolbar.contentView.textView.resignFirstResponder()
finishSendingMessage()
}
override func didPressAccessoryButton(sender: UIButton!) {
}
func addMessage(id: String, text: String) {
let message = JSQMessage(senderId: id, displayName: "Default Name", text: text)
messages.append(message)
}
func setupBubbles() {
let factory = JSQMessagesBubbleImageFactory()
outgoingBubbleImageView = factory.outgoingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleBlueColor())
incomingBubbleImageView = factory.incomingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleLightGrayColor())
}
//MARK: Delegate methods
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//让cell = super.collectionView(collectionView,cellForItemAtIndexPath:indexPath)为! JSQMessagesCollectionViewCell
let cell: JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if message.senderId == self.senderId {
cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
cell.textView.hidden = true
cell.messageBubbleContainerView
cell.textView.removeFromSuperview()
}
else {
cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
}
if message.senderId == self.senderId {
cell.textView.textColor = UIColor.whiteColor()
} else {
cell.textView.textColor = UIColor.blackColor()
}
return cell
}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let message = messages[indexPath.item]
// Check if the message is incoming or outgoing and generate the image accordingly
if message.senderId == senderId {
return outgoingBubbleImageView
} else {
return incomingBubbleImageView
}
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
// MARK: App lifecycle methods
override func viewDidLoad() {
self.senderId = "2"
self.senderDisplayName = "soe_random_display_name"
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupBubbles()
self.outgoingCellIdentifier = CustomOutgoingCell.cellReuseIdentifier()
self.collectionView.registerNib(CustomOutgoingCell.nib(), forCellWithReuseIdentifier: self.outgoingCellIdentifier)
//Remove the avatar images
self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero
finishReceivingMessage()
self.navigationController?.navigationItem.title = "RedCarpet"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我的CustomOutgoingCell.xib -
但是当我输入一条长信息时,会显示这一点,足以产生消息气泡。否则,文本字段将出现在消息气泡中。