gpt4 book ai didi

swift - 模型未表示的可见单元格后面的隐藏 TableViews 单元格

转载 作者:搜寻专家 更新时间:2023-10-31 21:45:48 25 4
gpt4 key购买 nike

我目前正在为客户构建聊天机器人应用程序。聊天是使用带有自定义单元格的 UITableView 实现的。来自机器人的文本消息构建为带有 UITextView 的自定义 UITableViewCell(因为它需要可点击链接,否则我会使用 UILabel)包裹在 UIView 容器中以在其周围绘制气泡。这个容器 View 也有一个阴影来取悦设计师:P

每次收到来自机器人的新消息时,此消息都会延迟一小部分时间,以便用户可以提前阅读消息。那时,一个所谓的 WaitingMessageTableViewCell 显示在新单元格将到达的索引处。时间到期后,WaitingMessageTableViewCell 从模型中移除,然后从 tableView 中移除,并插入新的单元格。

            tableView.beginUpdates()
if let insertRows = insertRows {
tableView.insertRows(at: insertRows, with: .none)
}
if let deleteRows = deleteRows {
tableView.deleteRows(at: deleteRows, with: .none)
}
if let reloadRows = reloadRows {
tableView.reloadRows(at: reloadRows, with: .none)
}
tableView.endUpdates()

这个问题是设计者发现的,当WaitingMessageTableViewCell即将出现时,之前cell的影子有一小段时间变强了。我调试进去,发现在插入WaitingMessageTableViewCell之前,它出现在之前的单元格后面,这对我来说毫无意义。(这是重用机制吗?!)现在出现了非常奇怪的部分:我使用了 Xcode 中内置的 UI 分析器,发现可见单元格后面有隐藏的单元格,这些单元格未由模型表示。

(lldb) po tableView.subviews
▿ 5 elements
▿ 0 : <CHAT.WaitingMessageTableViewCell: 0x7fdef286a600; baseClass = UITableViewCell; frame = (0 395.5; 375 40); hidden = YES; opaque = NO; autoresize = W; layer = <CALayer: 0x600003952d80>>
▿ 1 : <CHAT.AgentMessageTableViewCell: 0x7fdef2861e00; baseClass = UITableViewCell; frame = (0 294.5; 375 101); hidden = YES; opaque = NO; autoresize = W; layer = <CALayer: 0x6000039bf160>>
▿ 2 : <CHAT.AgentMessageTableViewCell: 0x7fdef3897600; baseClass = UITableViewCell; frame = (0 395.5; 375 101.5); opaque = NO; autoresize = W; layer = <CALayer: 0x6000039aba20>>
▿ 3 : <CHAT.AgentMessageTableViewCell: 0x7fdef382e200; baseClass = UITableViewCell; frame = (0 294.5; 375 101); opaque = NO; autoresize = W; layer = <CALayer: 0x600003955a00>>
▿ 4 : <CHAT.WelcomeTableViewCell: 0x7fdef2882c00; baseClass = UITableViewCell; frame = (0 0; 375 294.5); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x600003951160>>
(lldb) po tableView.visibleCells
▿ 3 elements
▿ 0 : <CHAT.WelcomeTableViewCell: 0x7fdef2882c00; baseClass = UITableViewCell; frame = (0 0; 375 294.5); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x600003951160>>
▿ 1 : <CHAT.AgentMessageTableViewCell: 0x7fdef382e200; baseClass = UITableViewCell; frame = (0 294.5; 375 101); opaque = NO; autoresize = W; layer = <CALayer: 0x600003955a00>>
▿ 2 : <CHAT.AgentMessageTableViewCell: 0x7fdef3897600; baseClass = UITableViewCell; frame = (0 395.5; 375 101.5); opaque = NO; autoresize = W; layer = <CALayer: 0x6000039aba20>>
(lldb) po AssistantDataManager.shared.cellModels
▿ 3 elements
▿ 0 : <WelcomeCellModel: 0x600002c80740>
▿ 1 : <SimpleAgentTextCellModel: 0x600001af0050>
▿ 2 : <AskQuestionCellModel: 0x600002c80400>

这也是我的 cellForRows 方法:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard indexPath.row < AssistantDataManager.shared.cellModels.count else {
return UITableViewCell()
}
let cellModel = AssistantDataManager.shared.cellModels[indexPath.row]

if let cell = tableView.dequeueReusableCell(withIdentifier: cellModel.cellReuseIdentifier) as? AssistantActionBaseTableViewCell {
//Quickfix for stronger shadow
cell.backgroundColor = Colors.tableViewBackgroundColor

cell.configure(with: cellModel)
if let topContraint = cell.topContraint {
topContraint.constant = calculateTopContraintSpacing(for: indexPath)
}

if let bottomContraint = cell.bottomContraint {
bottomContraint.constant = 0
}

cell.selectionStyle = .none
return cell
}
return UITableViewCell()
}

谁能告诉我为什么新插入的 WaitingMessageTableViewCell 首先在上面的单元格后面然后向下移动,为什么我的 tableView 填充了比它需要的更多的单元格?

非常感谢您。

最佳答案

我发现了为什么新单元格首先显示在前一个单元格后面然后向下移动。它是隐式动画和生命周期的结合。

之前我在一次批量更新中插入删除并重新加载所有单元格。由于隐式动画,tableView 决定为该更改设置动画。但是当我第一次插入和删除然后重新加载单元格时,一切都按预期工作。

tableView.performBatchUpdates({
if let insertRows = insertRows {
tableView.insertRows(at: insertRows, with: .none)
}
if let deleteRows = deleteRows {
tableView.deleteRows(at: deleteRows, with: .none)
}

}) { (finished) in
self.tableView.performBatchUpdates({
if let reloadRows = reloadRows {
self.tableView.reloadRows(at: reloadRows, with: .none)
}
}, completion: nil)
}

关于swift - 模型未表示的可见单元格后面的隐藏 TableViews 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56182562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com