尝试在Iphone上制作聊天应用程序,集成JSQMessagesViewController
时遇到问题。
这是我的“ ConversationViewController”的完整页面代码
import UIKit
import JSQMessagesViewController
class ConversationViewController : JSQMessagesViewController {
var myIndexPath:Int!
let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor(red: 63/255, green: 173/255, blue: 169/255, alpha: 1.0))
let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor(red: 230/255, green: 231/255, blue: 236/255, alpha: 1.0))
var messages = [JSQMessage]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setup()
self.addDemoMessages()
self.navigationItem.title = "\(myIndexPath)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func reloadMessagesView() {
self.collectionView?.reloadData()
}
}
//MARK - Setup
extension ConversationViewController {
func addDemoMessages() {
for i in 1...3 {
let sender = (i%2 == 0) ? "Server" : self.senderId
let messageContent = "Et eodem impetu Domitianum praecipitem per scalas itidem funibus constrinxerunt. \(i)"
let message = JSQMessage(senderId: sender, displayName: sender, text: messageContent)
self.messages += [message]
}
self.reloadMessagesView()
}
func setup() {
self.senderId = UIDevice.currentDevice().identifierForVendor?.UUIDString
self.senderDisplayName = UIDevice.currentDevice().identifierForVendor?.UUIDString
}
}
//MARK - Data Source
extension ConversationViewController {
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let data = self.messages[indexPath.row]
return data
}
override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
self.messages.removeAtIndex(indexPath.row)
}
// sender type bubble
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let data = messages[indexPath.row]
switch(data.senderId) {
case self.senderId:
return self.outgoingBubble
default:
return self.incomingBubble
}
}
// cell text color
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if message.senderId == "Server" {
cell.textView!.textColor = UIColor.blackColor()
} else {
cell.textView!.textColor = UIColor.whiteColor()
}
return cell
}
}
这会导致以下错误:
由于未捕获的异常而终止应用程序
“ NSInternalInconsistencyException”,原因:“错误:必需的方法
未实现:-[JSQMessagesViewController
collectionView:avatarImageDataForItemAtIndexPath:]
有人可以帮我吗?
谢谢
最佳答案
它是必需的,因此您只需要覆盖它并传回nil即可。
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource? {
return nil
}
您可以将其添加到同一文件中。
那你应该很好。
关于ios - 使用JSQMessagesViewController时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35273229/