我希望在单个单元格中显示两个数组值的表格视图。
假设我有两个数组,它们都包含相同数量的元素。
第一个数组和第二个数组。表格视图单元格中有两个标签Lbl1和Lbl2,现在Lbl1应该填充FirstArray,Lbl2应该填充SecondArray。我知道我们不能使用两个数组作为UITableView数据源。我想不出如何做到这一点。
请帮帮我。
我还尝试使用多个自定义表格视图单元格与部分。但它没有给出期望的结果。
我有两个数组 -
在 cellForRowAt 方法中:
第一个数组和第二个数组。表格视图单元格中有两个标签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
FirstCell
和SecondCell
。但是在你的问题中,你似乎暗示你只有一种单元格,而且这种单元格有两个标签? - SweeperindexPath.row % 2 == 0
,则使用datalist1进行初始化,否则使用datalist2进行初始化。就这样。