【发布时间】:2019-06-21 06:57:04
【问题描述】:
我想使用单个单元格在 tableview 中显示两个数组值。 假设我有两个数组,并且都包含相同的元素。
FirstArray 和 SecondArray。 tableview 单元格中有两个标签 Lbl1 和 Lbl2,现在 Lbl1 应该用 FirstArray 和 Lbl2 填充 应填写 SecondArray。我知道我们不能将两个数组用于 uitableview 数据源。我不知道该怎么做。
请帮帮我。
我还尝试使用多个自定义 tableview 单元格。但它没有给出预期的结果。
我有两个数组 -
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!
}
}
实际输出:
第一个单元格1 第一细胞2 第一单元3 第一单元4 第二单元1 第二单元2 第二单元3 第二单元4
预期输出:
第一个单元格1 第二单元1 第一细胞2 第二单元2 第一单元3 第二单元3 第一单元4 第二单元4
【问题讨论】:
-
您似乎有两种单元格 -
FirstCell和SecondCell。但在你的问题中,你似乎暗示你有一种细胞,那种细胞有 2 个标签? -
对于您的预期输出,您需要进行 2 处更改。 1. 保留单节并将 numberOfRowsInSection 加倍 2. 在 cellForRowAt 中,如果
indexPath.row % 2 == 0然后使用 datalist1 初始化,否则使用 datalist2 初始化。就是这样。 -
是的,我只希望在单个单元格中使用它,但为了获得预期的输出,我尝试使用两个单元格。@Sweeper
-
我尝试了@Pavan 建议的方式,但现在它抛出错误。
标签: ios swift uitableview custom-cell