【问题标题】:How to fill tableview with two arrays using single custom cell?如何使用单个自定义单元格用两个数组填充表格视图?
【发布时间】:2019-06-21 06:57:04
【问题描述】:

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

FirstArray 和 SecondArray。 tableview 单元格中有两个标签 Lbl1Lbl2,现在 Lbl1 应该用 FirstArrayLbl2 填充 应填写 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

【问题讨论】:

  • 您似乎有两种单元格 - FirstCellSecondCell。但在你的问题中,你似乎暗示你有一种细胞,那种细胞有 2 个标签?
  • 对于您的预期输出,您需要进行 2 处更改。 1. 保留单节并将 numberOfRowsInSection 加倍 2. 在 cellForRowAt 中,如果 indexPath.row % 2 == 0 然后使用 datalist1 初始化,否则使用 datalist2 初始化。就是这样。
  • 是的,我只希望在单个单元格中使用它,但为了获得预期的输出,我尝试使用两个单元格。@Sweeper
  • 我尝试了@Pavan 建议的方式,但现在它抛出错误。

标签: ios swift uitableview custom-cell


【解决方案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!
}

【讨论】:

  • 确保两个数组的对象数相等。
【解决方案2】:

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

案例 1:

Cell-1 : FirstCell1

Cell-2 : SecondCell1

Cell-3 : FirstCell2

Cell-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:

Cell-1 : FirstCell1 SecondCell1

Cell-2 : FirstCell2 SecondCell2

Cell-3 : FirstCell3 SecondCell3

Cell-4 : FirstCell4 SecondCell4

然后你可以实现如下:

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

}

【讨论】:

    【解决方案3】:

    你必须声明为

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        1.使用zip(_:_:)datalist1datalist2创建一个array,我们将把它用作dataSource for @ 987654326@.

        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
        }
        

        【讨论】:

          猜你喜欢
          • 2012-04-19
          • 1970-01-01
          • 1970-01-01
          • 2020-09-12
          • 1970-01-01
          • 2019-10-19
          • 2017-10-15
          • 2014-01-30
          • 1970-01-01
          相关资源
          最近更新 更多