Swift-JSQMessagesViewController-单元格自定义
我由此了解到,您可以通过重写以下方法自定义JSQMessagesViewController库的单元格:Swift-JSQMessagesViewController-单元格自定义,swift,custom-cell,jsqmessagesviewcontroller,Swift,Custom Cell,Jsqmessagesviewcontroller,我由此了解到,您可以通过重写以下方法自定义JSQMessagesViewController库的单元格: override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { //Configure your cell here return //yourCustomCell } 是否
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//Configure your cell here
return //yourCustomCell
}
是否有可能获得一个示例代码来理解如何实现实际的定制,包括JSQMessagesCollectionViewCell的子类
例如,我想在消息气泡底部添加一个标签,显示消息发送的时间
多谢各位
编辑
我创建了一个自定义单元类,如下所示:
class MyCustomCell: JSQMessagesCollectionViewCell {
var textLabel: UILabel
override init(frame: CGRect) {
self.textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height/3))
super.init(frame: frame)
self.textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
self.textLabel.textAlignment = .Center
contentView.addSubview(self.textLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "MyCustomCell")
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomCell
return cell
}
自定义单元格是以编程方式创建的,没有任何xib文件
因此,在我的JSQMessagesViewController中,我必须声明如下:
class MyCustomCell: JSQMessagesCollectionViewCell {
var textLabel: UILabel
override init(frame: CGRect) {
self.textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height/3))
super.init(frame: frame)
self.textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
self.textLabel.textAlignment = .Center
contentView.addSubview(self.textLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "MyCustomCell")
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomCell
return cell
}
然后,我可以覆盖cellForItemAtIndexPath方法,如下所示:
class MyCustomCell: JSQMessagesCollectionViewCell {
var textLabel: UILabel
override init(frame: CGRect) {
self.textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height/3))
super.init(frame: frame)
self.textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
self.textLabel.textAlignment = .Center
contentView.addSubview(self.textLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "MyCustomCell")
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomCell
return cell
}
问题是我再也看不到消息和以前的设置了
我做错了什么?转到xcode创建一个新文件,然后选择Cocoa Touch类并在子类列表中选择 UICollectionViewCell并将您的自定义单元格名称 customCellUICollectionViewCell.swift 然后转到主目录板,将一个新的集合单元格拖到视图中 选择您的单元格,然后在xcode的右侧菜单中选择 显示身份检查器 在自定义类中键入自定义类名称 customCellUICollectionViewCell.swift 收藏视图中的最后一件事
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! customCellUICollectionViewCell
return cell
}
我相信您只是没有设置自定义单元格的文本。我也会注意安全
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as? MyCustomCell else {
print("Could not get my custom cell")
return UICollectionViewCell()
}
let message = messages[indexPath.row] // or what ever your messages are called
let cell.mainLabel.text = message.text
return cell
}
我就是这样做到的: 1-创建两个子类:
- JSQMessagesCollectionViewCelloutGong
import UIKit import JSQMessagesViewController class messageViewOutgoing: JSQMessagesCollectionViewCellOutgoing { @IBOutlet weak var timeLabel: UILabel! required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } }
- JSQMessagesCollectionViewCellIncoming
import UIKit import JSQMessagesViewController class messageViewIncoming: JSQMessagesCollectionViewCellIncoming { @IBOutlet weak var timeLabel: UILabel! required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } }
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let message = self.messages[indexPath.item]
if message.senderId == self.senderId {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! messageViewOutgoing
cell.timeLabel.text = localHourFormatter.stringFromDate(message.date)
return cell
} else {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! messageViewIncoming
cell.timeLabel.text = localHourFormatter.stringFromDate(message.date)
return cell
}
}
现在,标签显示在气泡中
仍然需要做一些工作来调整气泡的大小,以便进行适当的显示,但这不是本问题的目的
如果有任何评论/问题,请告诉我。我收到错误:无法从MessageViewOuting转换为JSQMessageCollectionViewCellIncoming@sbkl。由于无法从父类型强制转换为子类型,您是否遇到此错误?我无法拖动新的集合单元格。整个收藏视图是由图书馆创建的。有人能帮我找到正确的方向吗?MyCustomCell如何保持父对象的所有行为和属性?似乎我对超类的init有问题…按照您在这里描述的做,但仍然在CellForRowatineXpath中发生崩溃“无法将类型为'JSQMessageCollectionViewCelloutGong'(0x10b5f3538)的值强制转换为'myApp.OutgoingMessageCell'(0x108cf9690)。“有什么想法吗?致命错误:使用未实现的初始值设定项'init(框架:)对于classHi的家伙们,很抱歉没有回应。你给出的反馈很难得到帮助。我放弃了这个库,转而使用react native进行移动开发。我将尝试找到我让它工作的项目,并在github上共享它。保持你的状态。如果你们仍然被上面的错误困扰,请点击这个链接。。我在cell=super.collectionView(collectionView,cellForItemAt:indexPath)行中遇到错误!messageViewOutgoing。错误为无法将“JSQMessageCollectionViewCelloutGong”类型的值强制转换为“messageViewOutgoing”。我的代码有什么问题如何在jsqmessagesviewcontroller中删除带有图像的单元格