我第一次在集合视图中使用DiffableDataSource,但是在试图更新视图时遇到了一个错误。
所以我正在使用两个定制的单元格。CustomCell1是一个单元格,左边有标签,右边有UITextField。CustomCell2是一个包含2列UIPickerView的单元格。CustomCell2有一个委托,它将在更新后让我的主视图控制器知道。一切都很顺利。
因此,当我试图用CustomCell1中的UIPickerView从CustomCell2中选择的值更新textfield时,问题就出现了。请注意,textfield用于页面行中的文本输入。单击单元格时显示选择器视图,再次单击单元格时显示隐藏的选择器视图。
因此,在我的委托中,要更新单元格,我有以下代码:
func updateSelectedCharacter(withCharacter character: String, indexPath: IndexPath) {
print(character)
DispatchQueue.main.async {
guard let charItem = self.dataSource.itemIdentifier(for: IndexPath(row: indexPath.row - 1, section: indexPath.section)), let parentId = charItem.parentId else { return }
self.updatePlayerData(forPlayerIndex: parentId, character: character)
var snap = self.dataSource.snapshot()
snap.reloadItems([charItem])
self.dataSource.apply(snap)
}
}在我的cellForRowAt中,我有以下代码来更新字符单元格(CustomCell1)中的文本字段。gameInfo对象是一个包含信息的结构,包括我试图显示的字符名,这些信息最终将从单元格中的条目存储到CoreData中。
guard let parentId = info.parentId else { return UICollectionViewListCell() }
let playerData = self.gameInfo.playerData[parentId]
var obj = TextEntryCellInformation(title: rowTitle, rowType: info.rowType)
obj.value = obj.value = playerData?.character
obj.isSelectable = false
return collectionView.dequeueConfiguredReusableCell(using: textEntryCell, for: indexPath, item: obj)下面是该单元格的单元格注册,其中TextEntryCellInformation保存行类型、标签标题和一个可以设置textfield文本的可选值:
private func createTextEntryListCellRegistration() -> UICollectionView.CellRegistration<TextEntryCollectionViewCell, TextEntryCellInformation> {
return UICollectionView.CellRegistration<TextEntryCollectionViewCell, TextEntryCellInformation> { cell, indexPath, data in
var config = TextEntryContentConfiguration()
config.title = data.title
config.tag = data.rowType.rawValue
config.textChangedDelegate = self
config.isSelectable = data.isSelectable
config.customPlaceholder = "required"
if let val = data.value {
config.textValue = val
}
cell.contentConfiguration = config
}
}所以现在,我想我已经解释了大部分代码。每当我在选择器视图中选择值时,第一次它将在字符单元格中正确地显示该值。但是如果我改变了这个值,文字就会从字符单元格中消失。但是,我正在从选择符视图中获得正确的值,并在委托函数开始时使用print调用进行验证。我还在验证在我应用了update委托函数之后,我正在运行单元格注册,并将正确的值分配给config.textValue。我不知道为什么,如果配置是数据设置与预期的正确的字符名称,为什么UI不更新,以显示该信息。
我包括了我认为是相关的代码和信息。不过,如果需要更多,我肯定会更新。
提前感谢您的帮助!
发布于 2022-02-12 21:21:36
结果我找到了答案。当在textfield自定义单元格(CustomCell1)的content视图上设置配置时,我正在进行检查以符合equatable:
static func == (lhs: TextEntryContentConfiguration, rhs: TextEntryContentConfiguration) -> Bool {
return lhs.title == rhs.title
}我需要做的是:
static func == (lhs: TextEntryContentConfiguration, rhs: TextEntryContentConfiguration) -> Bool {
return lhs.textValue == rhs.textValue
}这将检查差异,以确定是否应该更新配置值。
https://stackoverflow.com/questions/71095916
复制相似问题