我正在实施照片媒体项目并注意到一些极端的性能问题。当我开始调试时,我注意到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
答案 0 :(得分:0)