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

UITableView无法显示自定义类数组数据问题求助

问题排查与修复方案

1. UserTrips类初始化未绑定属性值

你的UserTrips类虽然在初始化方法里接收了参数,但没有将参数赋值给实例属性,导致所有实例的属性始终是默认值,无法存储自定义的行程数据:

错误代码:

init(type: String = "Tourism", roundtrip: String = "True", departure: String = "March 10, 2024", arrival: String = "March 23, 2024", from: String = "YYZ", to: String = "OPO", vehicle: String = "Air") {
}

修复后:

init(type: String = "Tourism", roundtrip: String = "True", departure: String = "March 10, 2024", arrival: String = "March 23, 2024", from: String = "YYZ", to: String = "OPO", vehicle: String = "Air") {
    self.type = type
    self.roundtrip = roundtrip
    self.departure = departure
    self.arrival = arrival
    self.from = from
    self.to = to
    self.vehicle = vehicle
}

2. UITableView单元格复用逻辑错误

在cellForRowAt方法中,你仅对indexPath.row > 0的行返回自定义单元格,第0行返回空白的默认单元格,这会导致第一行完全无内容,同时复用逻辑不符合规范:

错误代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let data = tripsArray[indexPath.row]
    
    if indexPath.row > 0 {
        let customCell = self.tripsCellView.dequeueReusableCell(withIdentifier: TripsTVC.identifier) as! TripsTVC
        customCell.configure(with: data.type, roundtrip: data.roundtrip, departure: data.departure, arrival: data.arrival, from: data.from, to: data.to, vehicle: data.vehicle)
        customCell.dateFrom?.text = data.departure
        customCell.dateTo?.text = data.arrival
        customCell.tripInformation?.text = "\(data.from) to \(data.to) // \(data.to) to \(data.from)"
        return customCell
    }
    
    return cell
}

修复后:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // 统一复用自定义单元格,无需区分行号
    let customCell = tableView.dequeueReusableCell(withIdentifier: TripsTVC.identifier, for: indexPath) as! TripsTVC
    let data = tripsArray[indexPath.row]
    
    // 用当前行数据填充单元格
    customCell.configure(with: data.type, 
                         roundtrip: data.roundtrip, 
                         departure: data.departure, 
                         arrival: data.arrival, 
                         from: data.from, 
                         to: data.to, 
                         vehicle: data.vehicle)
    
    // 根据往返状态动态显示行程文本
    let tripText = data.roundtrip == "True" ? "\(data.from) to \(data.to) // \(data.to) to \(data.from)" : "\(data.from) to \(data.to)"
    customCell.dateFrom?.text = data.departure
    customCell.dateTo?.text = data.arrival
    customCell.tripInformation?.text = tripText
    
    return customCell
}

3. 额外验证点

  • 确认TripsTVC的xib文件已正确关联,且identifier与代码中使用的完全一致
  • 检查tripsCellView的Auto Layout约束,确保没有被其他视图遮挡或高度设为0
  • 在viewDidLoad末尾添加self.tripsCellView.reloadData(),确保数据源设置后立即刷新表格

修复后核心代码片段

class UserTrips {
    var type: String
    var roundtrip: String
    var departure: String
    var arrival: String
    var from: String
    var to: String
    var vehicle: String
    
    init(type: String = "Tourism", roundtrip: String = "True", departure: String = "March 10, 2024", arrival: String = "March 23, 2024", from: String = "YYZ", to: String = "OPO", vehicle: String = "Air") {
        self.type = type
        self.roundtrip = roundtrip
        self.departure = departure
        self.arrival = arrival
        self.from = from
        self.to = to
        self.vehicle = vehicle
    }
}

// UITableView数据源方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tripsArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let customCell = tableView.dequeueReusableCell(withIdentifier: TripsTVC.identifier, for: indexPath) as! TripsTVC
    let data = tripsArray[indexPath.row]
    
    customCell.configure(with: data.type, 
                         roundtrip: data.roundtrip, 
                         departure: data.departure, 
                         arrival: data.arrival, 
                         from: data.from, 
                         to: data.to, 
                         vehicle: data.vehicle)
    
    let tripText = data.roundtrip == "True" ? "\(data.from) to \(data.to) // \(data.to) to \(data.from)" : "\(data.from) to \(data.to)"
    customCell.dateFrom?.text = data.departure
    customCell.dateTo?.text = data.arrival
    customCell.tripInformation?.text = tripText
    
    return customCell
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 14:20:00