gpt4 book ai didi

ios - 使用 JSQMessagesViewController 时出错

转载 作者:行者123 更新时间:2023-11-29 01:15:08 25 4
gpt4 key购买 nike

尝试在 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
}
}

这会导致以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'ERROR: required method not implemented: -[JSQMessagesViewController collectionView:avatarImageDataForItemAtIndexPath:]

有人可以帮我吗?谢谢

最佳答案

它是必需的,所以你只需要覆盖它并传回 nil。

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource? {
return nil
}

您可以将它添加到同一个文件中。那你应该很好。

关于ios - 使用 JSQMessagesViewController 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com