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

Swift UIKit中TableView加载Firestore单条字典数据仅显示首行求助

问题根源

你的核心逻辑错误是混淆了「Firestore返回的PlantData数组元素数量」和「单条PlantData需要在UITableView中展示的行数」:

  • 当前你只查询到1条PlantData数据,所以plantDataArray.count = 1,直接返回这个值作为行数的话,TableView只会渲染1行
  • 你要实现的需求是「1行自定义头部 + N行属性展示」,所有行的数据源都是这唯一的1条PlantData对象,和数组长度无关
修复步骤

1. 修改行数计算逻辑

你可以根据自己要展示的属性数量计算总行数,更灵活的写法是提前定义要展示的属性列表,后续新增属性不需要修改行数逻辑:

// 在控制器类内定义要展示的属性标题和对应KeyPath
let detailFields: [(title: String, valueKeyPath: KeyPath<PlantData, String>)] = [
    ("运行状态:", \.actualStatus),
    ("当前配方:", \.actualRecipe),
    ("设备ID:", \.plantId)
]

// 行数计算直接取属性列表长度,1是自定义头部的行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return plantDataArray.isEmpty ? 0 : 1 + detailFields.count
}

2. 优化Cell渲染逻辑

你原来的代码存在性能浪费:每次调用cellForRowAt都会同时创建自定义Cell和默认Cell,完全没必要,只需要根据当前行的位置创建对应类型的Cell即可。同时所有行的数据源都取plantDataArray的第一个元素即可,不要用indexPath.row去索引数组(数组只有第0位有值,索引超过0会直接崩溃)。

修复后的渲染代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // 所有行共用同一条PlantData数据
    guard let currentPlant = plantDataArray.first else {
        return UITableViewCell()
    }
    
    switch indexPath.row {
    case 0:
        // 第0行渲染自定义头部Cell
        let customCell = detailTableView.dequeueReusableCell(withIdentifier: MyDetailTableViewCell.identifier, for: indexPath) as! MyDetailTableViewCell
        customCell.selectionStyle = .none
        let imageName = currentPlant.plantType == "CIP" ? "CIP" : "PASTEURIZATION"
        customCell.configure(imageName: imageName)
        return customCell
    default:
        // 其余行渲染属性Cell
        let cell = detailTableView.dequeueReusableCell(withIdentifier: "DetailDefaultCell", for: indexPath)
        // 取当前行对应的属性配置,index减1是去掉头部的偏移量
        let field = detailFields[indexPath.row - 1]
        cell.textLabel?.text = field.title
        cell.detailTextLabel?.text = currentPlant[keyPath: field.valueKeyPath]
        cell.detailTextLabel?.textColor = .gray
        return cell
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 09:24:05