如何使用单个自定义单元格用两个数组填充表格视图?

How to fill tableview with two arrays using single custom cell?

我想在表格视图中使用单个单元格显示两个数组值。 假设我有两个数组并且都包含相同数量的元素。

FirstArray 和 SecondArray。表格视图单元格中有两个标签 Lbl1Lbl2,现在 Lbl1 应该填充 FirstArrayLbl2 应该用 SecondArray 填充。我知道我们不能为 uitableview 数据源使用两个数组。我不知道该怎么做。

请帮助我。

我也尝试过使用多个带有部分的自定义表格视图单元格。但它没有给出预期的结果。

我有两个数组 -

let datalist1 = ["firstCell1" , "firstCell2" , "firstCell3" , "firstCell4"]
    let datalist2 = ["secondCell1"  ,"secondCell2" , "secondCell3" ,"secondCell4"]

在表视图 numberOfRowsInSection 中:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0
        {
            return datalist1.count
        }
        else  {
            return datalist2.count
        }
    }

在 cellForRowAt 中:

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

        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? FirstCell
            cell?.initData(name: datalist1[indexPath.row])
            return cell!
        }
        else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? SecondCell
            cell?.initData(lbl: datalist2[indexPath.row])
            return cell!
        }

    }

实际输出:

FirstCell1 FirstCell2 FirstCell3 FirstCell4 SecondCell1 第二单元2 第二单元3 SecondCell4

预期输出:

FirstCell1 SecondCell1 FirstCell2 第二单元2 FirstCell3 第二单元3 FirstCell4 SecondCell4

您必须声明为

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

        if indexPath.section %2 == 0 {
            let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? FirstCell
            cell1.lbl1.text = datalist1[indexPath.row]
            return cell1!
        }
        else {
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? SecondCell            
            cell2.lbl2.text = datalist2[indexPath.row]
            return cell2!
        }

    }

您好,您不需要添加两个 section 只需按照以下步骤操作即可。 这是你的数组。

let datalist1 = ["firstCell1" , "firstCell2" , "firstCell3" , "firstCell4"]
let datalist2 = ["secondCell1"  ,"secondCell2" , "secondCell3" ,"secondCell4"]

行数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    return datalist1.coun
}

行单元格

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? FirstCell
    cell.Lbl1.text = datalist1[indexPath.row]
    cell.Lbl2.text = datalist2[indexPath.row]
    return cell!
}

根据你提供的解释和代码,需求不明确。 但是,根据以上的细节,可能有两种情况:

案例一:

Cell-1 : FirstCell1

Cell-2 : SecondCell1

单元格 3:第一个单元格 2

Cell-4 : SecondCell2

然后你可以像下面这样实现:

在表视图 numberOfRowsInSection 中:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (datalist1.count + datalist2.count)
}

在 cellForRowAt 中:

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

    if indexPath.row % 2 == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? FirstCell
        cell?.initData(name: datalist1[indexPath.row])
        return cell!
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? SecondCell
        cell?.initData(lbl: datalist2[indexPath.row])
        return cell!
    }

}

案例二:

单元格 1:第一个单元格 1 第二个单元格 1

单元格 2:第一个单元格 2 第二个单元格 2

Cell-3 : FirstCell3 SecondCell3

Cell-4 : FirstCell4 SecondCell4

然后你可以像下面这样实现:

在表视图 numberOfRowsInSection 中:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return datalist1.count
}

在 cellForRowAt 中:

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? FirstCell
        //Single custom cell can implement both the labels
        cell?.initData(name: datalist1[indexPath.row],lbl: datalist2[indexPath.row])
        return cell!
    }

}

最好的方法是使用模型。您必须声明数据模型,例如

struct MyModel {
   var firstValue: String?
   var secondValue: String?
}

然后您必须将两个数组转换为单个 MyModel 对象数组

var myData = Array<MyModel>()

然后通过使用 for 循环,您可以遍历一个数组并填充 myData 数组。

for (index, _) in datalist1 {
   let object = MyModel()
   object.firstValue = datalist1[index]
   object.firstValue = datalist2[index]
   myData.append(object)
}

然后只需实施 tableview 协议方法并使用 MyModel 对象填充您的自定义单元格。

1. 使用 [=] 从 datalist1datalist2 创建 单个 array 18=],我们将用作 dataSource for tableView.

lazy var dataList = Array(zip(self.datalist1, self.datalist2))

dataList 是类型 [(String, String)].

示例:

如果datalist1datalist2是,

let datalist1 = ["firstCell1" , "firstCell2" , "firstCell3" , "firstCell4"]
let datalist2 = ["secondCell1"  ,"secondCell2" , "secondCell3" ,"secondCell4"]

那么,dataList包含

[("firstCell1", "secondCell1"), ("firstCell2", "secondCell2"), ("firstCell3", "secondCell3"), ("firstCell4", "secondCell4")]

2. 您需要一个 cell 来显示所有数据。无需为此创建 2 个不同的 UITableViewCells。示例:

class CustomCell: UITableViewCell {
    @IBOutlet weak var lbl1: UILabel!
    @IBOutlet weak var lbl2: UILabel!
}

3. 现在,您的 UITableViewDataSource 方法看起来像,

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.lbl1.text = self.dataList[indexPath.row].0
    cell.lbl2.text = self.dataList[indexPath.row].1
    return cell
}