Chatto 自定义单元格高度

时间:2021-03-08 22:28:28

标签: ios swift

我正在研究 iOS 聊天和集成 Chatto 框架。 现在我需要添加我的自定义单元格,但问题是无法计算单元格自动调整大小的高度。 到目前为止我添加了

// PresenterBuilder
class CustomMessagePresenterBuilder: ChatItemPresenterBuilderProtocol {

    func canHandleChatItem(_ chatItem: ChatItemProtocol) -> Bool {
        return chatItem is CustomMessageModel
    }

    func createPresenterWithChatItem(_ chatItem: ChatItemProtocol) -> ChatItemPresenterProtocol {
        assert(self.canHandleChatItem(chatItem))
        return CustomMessagePresenter(customMessageModel: chatItem as! CustomMessageModel)
    }

    public var presenterType: ChatItemPresenterProtocol.Type {
        return CustomMessagePresenter.self
    }
}

//Presenter
class CustomMessagePresenter: ChatItemPresenterProtocol {

    let customMessageModel: CustomMessageModel
    init (customMessageModel: CustomMessageModel) {
        self.customMessageModel = customMessageModel
    }

    private static let cellReuseIdentifier = CustomMessageCollectionViewCell.self.description()

    class func registerCells(_ collectionView: UICollectionView) {
        collectionView.register(CustomMessageCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
    }

    let isItemUpdateSupported = false

    func update(with chatItem: ChatItemProtocol) {
    }


    var canCalculateHeightInBackground: Bool {
        return false
    }

    func cellWillBeShown(_ cell: UICollectionViewCell) {
        print(cell)
    }

    func heightForCell(maximumWidth width: CGFloat, decorationAttributes: ChatItemDecorationAttributesProtocol?) -> CGFloat {
        // How should I get dynamic sizing here?
        // sizingCell has no effect !!!
        return 200
    }

    func dequeueCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: CustomMessagePresenter.cellReuseIdentifier, for: indexPath)
    }

    func configureCell(_ cell: UICollectionViewCell, decorationAttributes: ChatItemDecorationAttributesProtocol?) {
        guard let customMessageCell = cell as? CustomMessageCollectionViewCell else {
            assert(false, "expecting status cell")
            return
        }

        customMessageCell.model = self.customMessageModel
    }
}


//Cell
class CustomMessageCollectionViewCell: UICollectionViewCell {

    private let imageView: UIImageView = UIImageView()
    private let lblMessage: UILabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }

    private func commonInit() {

        self.lblMessage.font = UIFont.systemFont(ofSize: 12)
        self.lblMessage.textAlignment = .center
        self.lblMessage.textColor = UIColor.gray
        self.lblMessage.numberOfLines = 0
        self.contentView.addSubview(lblMessage)

        self.imageView.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(self.imageView)
    }


    var model: CustomMessageModel? {
        didSet {
            self.updateCell(model)
        }
    }

    private func updateCell(_ model: CustomMessageModel?) {
        self.imageView.image = UIImage(named: model?.imageName ?? "")
        self.imageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.top.equalToSuperview()
            make.height.equalTo(100)
            make.width.equalTo(100)
        }

        self.lblMessage.text = model?.message
        self.lblMessage.snp.makeConstraints { (make) in
            make.top.equalTo(self.imageView.snp.bottom).offset(10)
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.leading.equalTo(self.contentView).offset(20)
            make.trailing.equalTo(self.contentView).offset(-20)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }
}

我知道文档中有一个关于如何添加自定义单元格的示例,但没有关于自动调整单元格高度的示例。 如果有人能提供一个例子就好了。

附言 没有“Chatto”标签,我也没有 1500 声望来创建它。

0 个答案:

没有答案