You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TableView删除指定分区行异常:点击分区2删除按钮清空全部内容

问题分析与修复方案

点击分区2删除按钮后整个TableView内容被清空,核心原因是数据源操作不规范、TableView代理方法未完善,结合你提供的代码,给出以下修复步骤:

1. 修正删除逻辑,避免数据源异常

当前deleteTableCell方法存在变量名拼写错误(attachmattachmentInfoDiagnosis应为attachmentInfoDiagnosis),且未校验索引合法性,容易触发运行时错误导致TableView异常刷新。修复后代码:

func deleteTableCell(index: Int) {
    // 先校验索引与数据源合法性,防止越界崩溃
    guard !attachmentTypeDiagnosis.isEmpty, index >= 0, index < attachmentTypeDiagnosis.count else {
        return
    }
    // 同步删除两个数据源的对应项
    attachmentTypeDiagnosis.remove(at: index)
    attachmentInfoDiagnosis.remove(at: index)
    // 使用批量更新保证数据源与UI同步
    tableviewDiagnosis.performBatchUpdates({
        tableviewDiagnosis.deleteRows(at: [IndexPath(row: index, section: 1)], with: .left)
    }, completion: nil)
}

2. 完善cellForRowAt方法,补充分区2的Cell创建

你的代码仅实现了分区0的Cell逻辑,分区2的Cell未处理,会导致TableView刷新时无法正确渲染内容,进而触发异常。补充后代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: DiagnosisListingTVCell.self)) as? DiagnosisListingTVCell else {
            return UITableViewCell()
        }
        cell.selectionStyle = .none
        let diagnosis = self.presenter?.emrRecord?.emrDetails?.emrDiagnosis?[indexPath.row]
        cell.lblTitle.attributedText = "\(diagnosis?.name ?? "")".style(using: .syne_regular_18_black)
        cell.lblDes.attributedText = "\(diagnosis?.description ?? "")".style(using: .syne_regular_14_grey)
        return cell
    } else {
        // 替换为你实际的分区2Cell类型
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AttachmentListingCell.self)) as? AttachmentListingCell else {
            return UITableViewCell()
        }
        cell.selectionStyle = .none
        // 设置分区2Cell的数据,示例:
        let attachment = attachmentTypeDiagnosis[indexPath.row]
        // cell.configure(with: attachment)
        return cell
    }
}

3. 规范头部视图的创建方式

当前用dequeueReusableCell创建头部视图不符合UITableView规范,会干扰Cell复用池,改用dequeueReusableHeaderFooterView:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: String(describing: DiagnosisTVHeaderCell.self)) as? DiagnosisTVHeaderCell else {
            return UITableViewHeaderFooterView()
        }
        header.delegate = self
        return header
    } else if section == 1 {
        guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: String(describing: EMRAttachmentsCell.self)) as? EMRAttachmentsCell else {
            return UITableViewHeaderFooterView()
        }
        header.delegate = self
        return header
    }
    return UIView()
}

同时需要在viewDidLoad中注册头部视图(而非Cell):

tableviewDiagnosis.register(DiagnosisTVHeaderCell.self, forHeaderFooterViewReuseIdentifier: String(describing: DiagnosisTVHeaderCell.self))
tableviewDiagnosis.register(EMRAttachmentsCell.self, forHeaderFooterViewReuseIdentifier: String(describing: EMRAttachmentsCell.self))

4. 检查代理回调的索引正确性

确保分区2删除按钮的代理回调中传递的index是分区2的行索引,而非全局索引,避免删除错误的数据源项导致数据混乱。

内容的提问来源于stack exchange,提问作者Shaikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:20:34