2017-07-19 72 views
0

我试图用两个部分实现tableview。每个部分都有一种需要的单元格。来自不同部分的TableView单元混合起来

因此,第一节单元被划分为PendingTVC 第二节单元被划分为ScheduledCell。

我有以下方法实施,但细胞越来越混合。例如,如果第一部分有3个单元格,则第2部分中的前3个单元格具有与第1部分的前3个单元格相关的混合标签。代码如下:

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


     if arr != nil { 
      if section == 0 { 
       print("returning pending : \(pendingCount)") 
       return pendingCount 
      } 
      else{ 
       print("returning scheduled count : \(scheduledCount)") 
       return scheduledCount 
      } 
     } 
     return 0 

    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

     if section == 0 { 
      return "Pending" 
     } 
     else{ 
      return "Scheduled" 
     } 

    } 

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 2 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let data = arr![indexPath.row] 
     if indexPath.section == 0 { 
      if let cell = tableView.dequeueReusableCell(withIdentifier: "Pending") as? PendingTVC{ 
       PendingTVC.formattCell(cell: cell, data: data) 
       cell.selectionStyle = .none; 
       cell.delegate = self 
       return cell 
      } 
     } 
     else{ 
      if let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledCell") as? ScheduledCell{ 
       print("cellforrowat in scheduledCell") 
       ScheduledCell.formatCell(cell: cell, data: data) 
       cell.selectionStyle = .none; 
       return cell 
      } 
     } 
     return UITableViewCell() 
    } 
+0

请附上截图,以便更好地理解您的问题。 – Surjeet

+0

你应该返回理想的单元格对象而不是UITableViewCell() – Ellen

+0

@ air6199这没有任何效果。我将我的数据源分成两个数组,而不是 – user2363025

回答

0

你应该在你的部分条件中获取你的数据。如果您事先获取数据,它与您需要的索引路径数据不一致。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{  
    if indexPath.section == 0 { 
     if let cell = tableView.dequeueReusableCell(withIdentifier: "Pending") as? PendingTVC{ 
      let data = pendingarr![indexPath.row] //access pending data here 
      PendingTVC.formattCell(cell: cell, data: data) 
      cell.selectionStyle = .none; 
      cell.delegate = self 
      return cell 
     } 
    } 
    else{ 
     if let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledCell") as? ScheduledCell{ 
      print("cellforrowat in scheduledCell") 
      let data = scheduledarr![indexPath.row] // access scheduled arr here 
      ScheduledCell.formatCell(cell: cell, data: data) 
      cell.selectionStyle = .none; 
      return cell 
     } 
    } 
} 
+0

它没有产生任何影响。你只是增加了一行代码。 – Surjeet

+0

挂起和计划都在同一个阵列 – user2363025

+0

@Surjeet我明白,如果有部分数据,他必须有字典或阵列数组。他需要先按部分访问数据。 – luckyShubhra

0

对于这两部分,您都使用相同的数据阵列。

让数据=编曲![indexPath.row]

在这里你可以看到你引用indexPath.row作为数据数组中的索引。这意味着该部分无关紧要。您至少有两个选项...

  1. 创建一个2d数组,其中第一个索引值是您的部分,第二个索引值是该行。例如dataArray = [[test,test2,test3],[otherTest,otherTest2,otherTest3]]。这可以通过更改线路访问:

    让数据= ARR [indexPath.section] [indexPath.row]

或 2.创建两个单独的阵列...一个用于第1部分,一个用于第2部分。在相关的部分检查中打电话给他们。