gpt4 book ai didi

swift - 尝试在 TableView 中快速查找特定单元格时失败

转载 作者:行者123 更新时间:2023-11-28 11:59:27 25 4
gpt4 key购买 nike

我想在我的 TableView 中找到特定的单元格类。我使用以下代码:

func setConfirmEnabledIfNeed(){
let ip = IndexPath(row: 8, section: 0)
if let cell = tableView.cellForRow(at: ip) as? ConfirmBtnCell {
print("find confirm cell")
}
let c = tableView.cellForRow(at: ip) as? ConfirmBtnCell
print("type of cell \(type(of: c))")
}

print("find confirm cell") 从未被调用,但是,第二个打印输出:type of cell Optional<ConfirmBtnCell> ,显然是我需要的。但是为什么不调用 first print 呢?

我的行单元格如下所示:

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

let item = viewModel.items[indexPath.row]
switch item {
case .selectable(let value, let placeholder, let type):
let selectionCell = tableView.dequeueReusableCell(withIdentifier: "SelectionCell") as! SelectionCell
if let placeholder = placeholder { selectionCell.setPlaceholder(placeholder) }
if let text = value as? String, !text.isEmpty { selectionCell.hidePlaceholder();
selectionCell.textLbl.text = text }
if let date = value as? Date { selectionCell.hidePlaceholder();
selectionCell.textLbl.text = DateFormatterUtil.getReadableStringFromDate(date) }
return selectionCell
case .back:
return tableView.dequeueReusableCell(withIdentifier: "BackBtnCell") as! BackBtnCell
case .confirm:
return tableView.dequeueReusableCell(withIdentifier: "ConfirmBtnCell") as! ConfirmBtnCell
case .editableNumbers(let text, let placeholder, let type):
let editableCell = tableView.dequeueReusableCell(withIdentifier: "TextEditCell") as! TextEditCell
editableCell.setup(isNumerical: true)
if let placeholder = placeholder { editableCell.setPlaceholder(placeholder) }
if let text = text {editableCell.hidePlaceholder(); editableCell.txtf.text = text }
editableCell.textChanged = {[weak self] text in
guard let text = text else { return }
if type == .sumCash {
self?.viewModel.collectedInfo[CorrectionChequeViewModel.infoKeys.sumCash.rawValue] = text
self?.setConfirmEnabledIfNeed()
}

if type == .sumElectronic {
self?.viewModel.collectedInfo[CorrectionChequeViewModel.infoKeys.sumElectronic.rawValue] = text
}
}
return editableCell
case .editableText(let text, let placeholder, let type):
let editableCell = tableView.dequeueReusableCell(withIdentifier: "TextEditCell") as! TextEditCell
editableCell.setup(isNumerical: false)
if let placeholder = placeholder { editableCell.setPlaceholder(placeholder) }
if let text = text {editableCell.hidePlaceholder(); editableCell.txtf.text = text }
editableCell.textChanged = {[weak self] text in
guard let text = text else { return }
if type == .sumCash {
self?.viewModel.collectedInfo[CorrectionChequeViewModel.infoKeys.description.rawValue] = text
}

if type == .sumElectronic {
self?.viewModel.collectedInfo[CorrectionChequeViewModel.infoKeys.number.rawValue] = text
}
}
return editableCell
default:
return UITableViewCell()
}
}

更新:我发现如果不从 TableView cellForRow 方法调用代码,它就可以工作。我尝试使用 dispatch_after 启动该代码块,它起作用了。

最佳答案

在你的if语句,tableView.cellForRow(at: ip) as? ConfirmBtnCell 的值是零,所以你永远不会进入 block 。

在第二条语句(let c = tableView.cellForRow(at: ip) as? ConfirmBtnCell)中,c 的值是一个 Optional<ConfirmBtnCell> .如果你解包可选的,你会看到它的解包值也是一个 nil。

如果您的行不可见,tableView.cellForRow即使设置了单元格,也会返回 nil。查看Apple Documentation .

关于swift - 尝试在 TableView 中快速查找特定单元格时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354115/

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