Ios 从2个不同的阵列加载tableview

Ios 从2个不同的阵列加载tableview,ios,swift,uitableview,core-data,Ios,Swift,Uitableview,Core Data,我有2个coredata数组。一个有3个元素,另一个也有3个元素。现在我想在tableview中加载这两个数组。因此,我的tableview中总共有6行 这就是我所取得的成就 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let totalCount = (customerDetails.count) + (customerDetails2.count)

我有2个coredata数组。一个有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
        }
    }
let customers=customerDetails[indexPath.row]//此处崩溃

对于0..中的i,将cellforRow中的代码更改为此,请注意

 let arr1Count = customerDetails.count

  if(indexPath.row < = arr1Count )
 {
     let customers = customerDetails[indexPath.row]
      cell.nameLabel.text = customers.fname
 }
else

{
     let customers = customerDetails2[indexPath.row - arr1Count]
      cell.nameLabel.text = customers.fname

 }
让arr1Count=customerDetails.count
if(indexPath.row<=arr1Count)
{
let customers=customerDetails[indexPath.row]
cell.namelab.text=customers.fname
}
其他的
{
let customers=customerDetails2[indexPath.row-arr1Count]
cell.namelab.text=customers.fname
}
如果indexPath.row
您可以将这两个部分分为两个部分,而不是仅用一个部分创建它:

let sections = [customerDetails,customerDetails2]
在numberOfSections中,您可以提供计数:

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}
之后,在numberOfItemsInSection中,您可以根据节号提供相应的数组:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
完成此操作后,您可以轻松访问并向cellForRow提供数据,如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let customer = sections[indexPath.section][indexPath.row]
     cell.nameLabel.text = customer.name
}

希望能有帮助

谢谢你的回答…接受之前给出的另一个答案…已经投了赞成票…:)很乐意帮忙@v、 b您还可以使用Enum定义每个部分,使其更具描述性。完美。请接受答案以备将来参考。当然!!所以不允许这么快就接受答案…:我完全理解。曾经在那里:)@v.bw:我感激你拯救了我的世界,男人!也感谢您的贡献@Sh_Khan..投了一票...)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let customer = sections[indexPath.section][indexPath.row]
     cell.nameLabel.text = customer.name
}