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

3
我希望在单个单元格中显示两个数组值的表格视图。 假设我有两个数组,它们都包含相同数量的元素。
第一个数组和第二个数组。表格视图单元格中有两个标签Lbl1和Lbl2,现在Lbl1应该填充FirstArray,Lbl2应该填充SecondArray。我知道我们不能使用两个数组作为UITableView数据源。我想不出如何做到这一点。
请帮帮我。
我还尝试使用多个自定义表格视图单元格与部分。但它没有给出期望的结果。
我有两个数组 -
let datalist1 = ["firstCell1" , "firstCell2" , "firstCell3" , "firstCell4"]
    let datalist2 = ["secondCell1"  ,"secondCell2" , "secondCell3" ,"secondCell4"]

在tableview的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 SecondCell2 SecondCell3 SecondCell4

期望输出:

FirstCell1 SecondCell1 FirstCell2 SecondCell2 FirstCell3 SecondCell3 FirstCell4 SecondCell4


看起来你有两种单元格 - FirstCellSecondCell。但是在你的问题中,你似乎暗示你只有一种单元格,而且这种单元格有两个标签? - Sweeper
4
为了得到您期望的输出结果,您需要进行两个更改:
  1. 保留单一部分,并将numberOfRowsInSection加倍。
  2. 在cellForRowAt方法中,如果indexPath.row % 2 == 0,则使用datalist1进行初始化,否则使用datalist2进行初始化。就这样。
- Pavan Kotesh
是的,我只想要一个单元格,但为了获得预期的输出,我尝试使用了两个单元格。@Sweeper - PUJA SINGH
我尝试了@Pavan建议的方法,但现在它报错了。 - PUJA SINGH
5个回答

1

你不需要添加两个 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!
}

请确保两个数组的对象数量相等。 - Harsh Pipaliya

1
根据您提供的解释和代码,要求不够清晰。但是,基于上述细节,可能有两种情况:
案例1:
单元格1:FirstCell1
单元格2:SecondCell1
单元格3:FirstCell2
单元格4:SecondCell2
然后,您可以实现如下内容:
在tableview的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!
    }

}

案例-2:

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

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

单元格3:第一单元格3 第二单元格3

单元格4:第一单元格4 第二单元格4

然后您可以实现类似以下的内容:

在tableview的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!
    }

}

0

你需要声明为

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!
        }

    }

0

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

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对象填充您的自定义单元格。


0
1. 使用 zip(_:_:)datalist1datalist2 创建一个单一的array,我们将使用它作为tableViewdataSource
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来显示所有这些数据,没有必要为此创建两个不同的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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接