gpt4 book ai didi

ios - 从 2 个不同的数组加载 tableview

转载 作者:搜寻专家 更新时间:2023-10-31 22:18:19 25 4
gpt4 key购买 nike

我有 2 个核心数据数组。一个有 3 个元素,另一个也有 3 个元素。现在我想在我的 TableView 中加载这两个数组。所以我的 tableview 中总共有 6 行。

我是这样实现的...

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let totalCount = (customerDetails.count) + (customerDetails2.count)
return totalCount
}

这两个数组的数据我尝试像这样加载到 cellForRowAt...

    let customers = customerDetails[indexPath.row]   //CRASHES HERE
for i in 0..<(customerDetails.count) {
cell.nameLabel.text = customers.fname
if i == customerDetails.count {
break
}
}
let customers2 = customerDetails2[indexPath.row]
for i in 0..<(customerDetails2.count) {
cell.nameLabel.text = customers2.fname
if i == customerDetails2.count {
break
}
}

但它在提到的那行崩溃说 Index out of range 可能是因为 customerDetails 只有 3 行,而加载的总单元格是 6。

在这种情况下可以做什么......?

最佳答案

if indexPath.row < customerDetails.count
{
// load from customerDetails array
let customer = customerDetails[indexPath.row]
}
else
{
// load from customerDetails2 array
let customer = customerDetails2[indexPath.row - customerDetails.count]
}

关于ios - 从 2 个不同的数组加载 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48748462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com