Ios 如何将JSQDataSourcesKit与JSQMessagesViewController一起使用

Ios 如何将JSQDataSourcesKit与JSQMessagesViewController一起使用,ios,core-data,jsqmessagesviewcontroller,Ios,Core Data,Jsqmessagesviewcontroller,我一直在尝试使用JSQMessagesViewController和JSQDataSourcesKit构建一个聊天应用程序,以管理核心数据中的数据。我已经找到了一些参考资料,说这是可能的,但我不能让它为我工作。我有应用程序构建等功能,但当我尝试加载消息时,会出现崩溃: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JSQDataSourcesKit.BridgedDat

我一直在尝试使用JSQMessagesViewController和JSQDataSourcesKit构建一个聊天应用程序,以管理核心数据中的数据。我已经找到了一些参考资料,说这是可能的,但我不能让它为我工作。我有应用程序构建等功能,但当我尝试加载消息时,会出现崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JSQDataSourcesKit.BridgedDataSource collectionView:messageDataForItemAtIndexPath:]: unrecognized selector sent to instance 0x1024e49c0'
我的代码是:

import UIKit
import JSQMessagesViewController
import CoreData
import MagicalRecord
import JSQDataSourcesKit


@objc class MessagesViewController: JSQMessagesViewController,NSFetchedResultsControllerDelegate {

let delegate = UIApplication.sharedApplication().delegate as! AppDelegate

typealias MessageCellFactory = CellFactory<Message, JSQMessagesCollectionViewCell>
typealias MessageSupplementaryViewFactory = ComposedCollectionSupplementaryViewFactory<Message>
var dataSourceProvider: CollectionViewFetchedResultsDataSourceProvider<MessageCellFactory, MessageSupplementaryViewFactory>?

var delegateProvider: CollectionViewFetchedResultsDelegateProvider<MessageCellFactory>?

var frc : NSFetchedResultsController?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let user :NSDictionary = delegate.getMatchedUserForUsername(self.senderId)
    self.senderDisplayName = user.objectForKey("full_name") as! String;



    // 1. create cell factory
    let cellFactory = CellFactory(reuseIdentifier: "cell") { (cell, model: Message, collectionView, indexPath) -> JSQMessagesCollectionViewCell in

        cell.textView.text = model.text;
        return cell
    }



    // 2. create supplementary view factory
    let headerFactory = TitledCollectionReusableViewFactory(
        dataConfigurator: { (header, item: Message?, kind, collectionView, indexPath) -> TitledCollectionReusableView in

            return header
        },
        styleConfigurator: { (header) -> Void in
            header.backgroundColor = .darkGrayColor()
    })

    let footerFactory = TitledCollectionReusableViewFactory(
        dataConfigurator: { (footer, item: Message?, kind, collectionView, indexPath) -> TitledCollectionReusableView in

            return footer
        },
        styleConfigurator: { (footer) -> Void in
            footer.backgroundColor = .lightGrayColor()
            footer.label.font = .preferredFontForTextStyle(UIFontTextStyleFootnote)
            footer.label.textAlignment = .Center
    })

    let composedFactory = ComposedCollectionSupplementaryViewFactory(headerViewFactory: headerFactory, footerViewFactory: footerFactory)

    // 3. create fetched results controller
    frc = messageFRCinContext(NSManagedObjectContext.MR_defaultContext())

    // 4. create delegate provider
    delegateProvider = CollectionViewFetchedResultsDelegateProvider(collectionView: collectionView!,
                                                                    cellFactory: cellFactory,
                                                                    fetchedResultsController: frc!)

    // 5. create data source provider
    dataSourceProvider = CollectionViewFetchedResultsDataSourceProvider(fetchedResultsController: frc!,
                                                                        cellFactory: cellFactory,
                                                                        supplementaryViewFactory: composedFactory,
                                                                        collectionView: collectionView)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
    let msg : Message = frc?.objectAtIndexPath(indexPath) as! Message
    let messageData = JSQMessage(senderId: msg.sender_jid, displayName: "", text: msg.text)
    return messageData
}

func messageFRCinContext(context: NSManagedObjectContext) -> NSFetchedResultsController {
    return  Message.MR_fetchAllSortedBy("sent_timestamp", ascending: false, withPredicate: nil, groupBy: nil, delegate: self)
}

}
导入UIKit
导入JSQMessagesViewController
导入CoreData
导入MagicalRecord
导入JSQDataSourcesKit
@objc类MessagesViewController:JSQMessagesViewController,NSFetchedResultsControllerDelegate{
让delegate=UIApplication.sharedApplication()。委托为!AppDelegate
typealias MessageCellFactory=CellFactory
typealias MessageSupplementaryViewFactory=ComposedCollectionsSupplementaryViewFactory
var数据源提供程序:CollectionViewFetchedResultsDataSourceProvider?
var delegateProvider:CollectionViewFetchedResultsDelegateProvider?
var frc:NSFetchedResultsController?
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
让用户:NSDictionary=delegate.getMatchedUserForUsername(self.senderId)
self.senderDisplayName=user.objectForKey(“全名”)为!字符串;
//1.创建单元工厂
让cellFactory=cellFactory(reuseIdentifier:“cell”){(cell,model:Message,collectionView,indexPath)->中的JSQMessageCollectionViewCell
cell.textView.text=model.text;
返回单元
}
//2.创建辅助视图工厂
让headerFactory=标题CollectionReusableViewFactory(
dataConfigurator:{(标头,项:消息?、种类、collectionView、indexPath)->中的标题CollectionReusableView
回流集管
},
样式配置器:{(标头)->中的Void
header.backgroundColor=.darkGrayColor()
})
让footerFactory=标题CollectionReusableViewFactory(
dataConfigurator:{(页脚,项:消息?、种类、collectionView、indexPath)->中的标题CollectionReusableView
返回页脚
},
样式配置器:{(页脚)->中的Void
footer.backgroundColor=.lightGrayColor()
footer.label.font=.preferredFontForTextStyle(UIFontTextStyleFootnote)
footer.label.textAlignment=.Center
})
让composedFactory=ComposedCollectionsSupplementaryViewFactory(HeadServiceEWFactory:headerFactory,FooteViewFactory:footerFactory)
//3.创建获取的结果控制器
frc=messageFRCinContext(NSManagedObjectContext.MR_defaultContext())
//4.创建委托提供程序
delegateProvider=CollectionViewFetchedResultsDelegateProvider(collectionView:collectionView!,
cellFactory:cellFactory,
fetchedResultsController:frc!)
//5.创建数据源提供程序
dataSourceProvider=CollectionViewFetchedResultsDataSourceProvider(fetchedResultsController:frc!,
cellFactory:cellFactory,
补充视图工厂:复合工厂,
collectionView:collectionView)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
重写func collectionView(collectionView:JSQMessageCollectionView!,messageDataForItemAtIndexPath indexPath:NSIndexPath!)->JSQMessageData{
将msg:Message=frc?.objectAtIndexPath(indexPath)设为!Message
让messageData=JSQMessage(senderId:msg.sender\u jid,displayName:,text:msg.text)
返回消息数据
}
func messageFRCinContext(上下文:NSManagedObjectContext)->NSFetchedResultsController{
返回消息。MR_fetchAllSortedBy(“sent_timestamp”,升序:false,带谓词:nil,groupBy:nil,委托:self)
}
}
看起来数据源需要符合JSQMessageData的对象,但是CollectionViewFetchedResultsDataSourceProvider没有相关的方法。我如何将两者连接起来?我是否需要对数据源类进行子类划分?有人用这个吗


谢谢。

嘿,我们能帮你弄明白吗??我就被困在这里,这最终是不可能的。我要做的是使用一个NSFetchedResultsController,它最终表现得非常好。嘿,我们能解决这个问题吗??我就被困在这里,这最终是不可能的。我要做的是使用一个NSFetchedResultsController,它最终表现得非常好。