需要具有不同行的多个tableview单元格

时间:2018-04-02 17:53:03

标签: xcode uitableview swift3 tableview swift4

根据您选择的标签,我有一个使用不同单元格的表格视图。它正在发挥作用,虽然第一个似乎控制了高度。我在第一个标签中有一行,但在第二个标签中有2行,两个都只显示1。 我想知道是否需要在选项卡选择或类似的东西上清除dequeueReusableCell。任何想法?

enter image description here

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  if activeTab == "tab1" {
    return someService.tab1.count
  } else {
    return someService.tab2.count
  }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if activeTab == "tab1" {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Tab1Cell") as? Tab1Cell {
        let tab1 = someService.tab1[indexPath.row]
        cell.updateView(tab1: tab1)

        return cell
    } else {
        return Tab1Cell()
    }
} else {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Tab2Cell") as? Tab2Cell {
        let tab2 = someService.tab2[indexPath.row]
        cell.updateView(tab2: tab2)

        return cell
    } else {
        return Tab2Cell()
    }
  }
}

我还用这个修复了滚动:

func fixTableViewSize() {
  let cells = self.notesTable.visibleCells
  var heightOfTableView = 0
  for cell in cells {
    print("cell")
    heightOfTableView += Int(cell.frame.height)
  }
  self.tableViewHeightConstraint.constant = CGFloat(heightOfTableView)
  self.buttonFromTableTopConstraint.constant = CGFloat(heightOfTableView)
}

0 个答案:

没有答案