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

如何在CarPlay应用列表中添加ProgressBar展示停车场使用率?

CarPlay停车场列表添加进度条可视化方案

要在CarPlay的列表项中添加进度条展示停车场使用率,你可以利用CPListItem的progress属性来实现,具体修改步骤如下:

核心修改点

  • 计算使用率:基于总车位和空闲车位算出已使用比例(需避免总车位为0的除以0错误)
  • 为CPListItem设置progress属性,传入0~1之间的进度值
  • 可保留或调整详情文本,兼顾数值展示和可视化效果

修改后的完整代码

var items: [ParkingGarage] = []
do {
    items = try getData()
} catch {
    print(error)
}
var sections: [CPListSection] = []
let listItems = items.map { item in
    // 计算使用率:已使用车位/总车位,处理总车位为0的边界情况
    let progress: Double
    if item.allPlaces > 0 {
        progress = Double(item.allPlaces - item.freePlaces) / Double(item.allPlaces)
    } else {
        progress = 0.0
    }
    
    // 创建列表项,保留名称,可调整详情文本(这里保留原空闲车位信息)
    let cell = CPListItem(text: item.name, 
                          detailText: "Free: \(item.freePlaces)/\(item.allPlaces)", 
                          image: UIImage(systemName: "parkingsign.circle")?.withTintColor(.systemPink), 
                          accessoryImage: .none, 
                          accessoryType: .none)
    // 设置进度条
    cell.progress = progress
    cell.handler = { cell, completion in
        completion()
    }
    return cell
}
let section = CPListSection(items: listItems)
sections.append(section)

// 创建列表模板并设置为根模板
let listTemplate = CPListTemplate(title: "ParkenUlm", sections: sections)
interfaceController?.setRootTemplate(listTemplate, animated: true)

额外说明

  • 如果希望进度条展示空闲车位比例,只需把进度计算改为Double(item.freePlaces)/Double(item.allPlaces)
  • CPListItem的进度条会自动适配CarPlay的UI样式,无需额外自定义视图
  • 务必处理总车位为0的情况,防止运行时崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 11:38:08