Swift - JSQMessagesViewController - 自定义单元格

标签 swift custom-cell jsqmessagesviewcontroller

我从这里了解到link您可以通过覆盖此方法来自定义 JSQMessagesViewController 库的单元格:

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")
    }

}

自定义单元格是在没有任何 xib 文件的情况下以编程方式制作的。

所以在我的 JSQMessagesViewController 中,我必须声明如下:

override func viewDidLoad() {

    super.viewDidLoad()

    self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "MyCustomCell")
}

然后,我可以如下覆盖 cellForItemAtIndexPath 方法:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomCell
    return cell
}

问题是我无法再看到消息和以前的设置。

我做错了什么?

最佳答案

这是我设法做到的方式:

1- 创建两个子类:

  • JSQMessagesCollectionViewCellOutgoing

    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)        
        }
    }
    

2- 创建两个 xib 文件,其名称与上述两个子类相关。对于每个文件,在文件所有者占位符中添加相关的自定义类。

3- 从存储在 Resources 文件夹中的 JSQMessagesViewController 库中复制/粘贴每个相关类的 xib 模板,并将您的自定义标签添加到其中。

4- 将自定义标签链接到上面的新子类<​​/p>

5- 覆盖下面的函数

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
    }
}

现在标签显示在气泡中。

要调整气泡的大小以正确显示,还有一些工作要做,但这不是这个问题的目的。

如果有任何意见/问题,请告诉我。

关于Swift - JSQMessagesViewController - 自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37788924/

相关文章:

ios - Swift 中没有 dequeueReusableCellWithIdentifier 的自定义表格单元格

ios - 使用工厂模式在同一个表中加载不同的自定义单元格

swift - 显示用户位置

swift - 重复消息错误

swift - 无法在 Swift Playgrounds 上捕获视频数据,未调用 captureOutput AVCaptureVideoDataOutputSampleBufferDelegate 委托(delegate)方法

swift - 在苹果 Swift 中使用巨大的数字

swift 扩展: augment existing class with method overloading

swift - 单击单元格按钮时应用程序崩溃

swift - 更改聊天气泡中的 TextView 颜色

ios - 设置选项 View 属性的属性时出现 EXC_BAD_ACCESS