swift CollectionView中单元格内的TableView

bsxbgnwa  于 2023-08-02  发布在  Swift
关注(0)|答案(1)|浏览(132)

我有一个collectionView与“聊天一样”布局。
这是我在聊天中“发送消息”的方式:

private func sendMessage(message: ChatMessage) {
        self.collectionView.performBatchUpdates({ [weak self] in
            let indexPath = IndexPath(item: self?.messages.count ?? 0, section: 0)
            self?.messages.append(message)
            self?.collectionView.insertItems(at: [indexPath])
        }) { [weak self] _ in
            self?.scrollToBottom(animated: true)
        }
    }

字符串
另外,我还为这个collectionView创建了一个单元格,其中包含TableView,用于表示问题的可能答案。对于tableView estimatedRowHeight == 44,isScrollEnabled = false和rowHeight = UITableView.automaticDimension。这是我计算单元格大小的方法:

@discardableResult
    func layout() -> CGFloat {
        let tableViewSize = tableView.sizeThatFits(CGSize(width: ChatCellConstants.textMaxSize, height: .greatestFiniteMagnitude))
        let titleSize = titleLabel.sizeThatFits(CGSize(width: ChatCellConstants.textMaxSize, height: .greatestFiniteMagnitude))
        let contentWidth = max(tableViewSize.width, titleSize.width)
        let tableViewHeight = answers.reduce(0) { partialResult, data in
            return partialResult + cell.set(text: data.title, isSelected: false, animated: false).layoutHeight()
        }
        
        if isMyMessage {
            tableView.layout(
                right: right - ChatCellConstants.textHOffset,
                top: titleLabel.bottom + ChatCellConstants.standartOffset,
                width: contentWidth,
                height: tableViewHeight
            )
            chatBubleView.backgroundColor = AppTheme.shared.colors.purple.withAlphaComponent(0.6)
        } else {
            tableView.layout(
                top: titleLabel.bottom + ChatCellConstants.standartOffset,
                left: left,
                width: contentWidth,
                fitHeight: tableViewHeight
            )
            chatBubleView.backgroundColor = AppTheme.shared.colors.cian.withAlphaComponent(0.6)
        }
        titleLabel.layout(left: tableView.left, right: tableView.right, top: ChatCellConstants.standartOffset, height: titleSize.height)
        checkButton.layout(left: tableView.left, right: tableView.right, top: tableView.bottom + ChatCellConstants.standartOffset, height: 48)

        chatBubleView.layout(
            left: tableView.left - ChatCellConstants.standartOffset,
            right: tableView.right + ChatCellConstants.standartOffset,
            top: titleLabel.top - ChatCellConstants.standartOffset,
            bottom: checkButton.bottom + ChatCellConstants.standartOffset
        )
        return chatBubleView.bottom
    }


问题如下:当问题的答案选项的文本太大时,tableView会被裁剪,并且所有必要的单元格都不会显示在其中。如果您将此单元格从屏幕上滚动出去,然后再次返回到该单元格,则会开始显示它们。
我期望当相应的单元格出现在collectionView中时,tableView将立即以完整大小显示。

的数据

chy5wohz

chy5wohz1#

您可以使用自动布局来计算UITableView高度。下面是一个例子:

//Create a subclass of UITableView
class SizebleTableView: UITableView {
    override var intrinsicContentSize: CGSize {
        // This way the width is defined by the frame
        // And height value is taken from content size of
        CGSize(width: frame.width, height: contentSize.height)
    }
}

字符串
然后,无论你在哪里更新表,你都应该调用invalidateIntrinsicContentSize()

@IBAction private func addItem(_ sender: UIButton) {
    items.append("Aditional item #\(items.count + 1)")
    tableView.reloadData()
    tableView.invalidateIntrinsicContentSize()
}


我认为你需要禁用滚动,反弹和滚动指标,使它看起来像正常的看法。
结果如下:TableView

相关问题