尝试在表视图中快速查找特定单元格时失败

时间:2018-05-15 15:34:51

标签: swift uitableview

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

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(“查找确认单元格”)永远不会被调用,但是,第二次打印输出:type of cell Optional<ConfirmBtnCell>,显然我需要什么。但为什么第一次打印不叫?

我的行的单元格如下所示:

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()
    }
}

更新: 如果没有从表视图cellForRow方法调用代码,我发现该代码有效。我尝试使用dispatch_after启动该代码块,并且它可以工作。

1 个答案:

答案 0 :(得分:2)

if语句中,tableView.cellForRow(at: ip) as? ConfirmBtnCell的值为nil,因此您永远不会输入该块。

在第二个声明(let c = tableView.cellForRow(at: ip) as? ConfirmBtnCell)中,c的值为Optional<ConfirmBtnCell>。如果打开可选项,您将看到其未包装的值也是零。

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