我正在实施照片媒体项目,并注意到一些极端的性能问题。当我开始调试时,我注意到messageDataForItemAtIndexPath
每个项目被调用 4 次,而在我设置的另一个示例项目中,它被调用 10 次
这是我设置来测试的一个虚拟项目,即使只有一个消息项,它也会调用每个项目 10 次。
这给我带来了问题,因为这是我开始异步下载媒体照片并为每个项目调用下载媒体请求 10 次的功能,这给我带来了问题。
我在这里误解了一些基本的东西吗?
import UIKit
import JSQMessagesViewController
class ViewController: JSQMessagesViewController {
var messages = [JSQMessage]()
override func viewDidLoad() {
super.viewDidLoad()
self.senderId = "1"
self.senderDisplayName = "me"
let message = JSQMessage(senderId: "admin", displayName: "Admin", text:"hello from admin")
self.messages.append(message)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.blueColor())
}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
//why is this called 10 times?
let msg = self.messages[indexPath.item];
let text = msg.text
print(text + ":" + String(indexPath.item))
return JSQMessage(senderId: msg.senderId, displayName: msg.senderDisplayName, text: text)
}
}
这将在控制台中输出
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0