【问题标题】:Different height for multiple prototype cells多个原型单元的不同高度
【发布时间】:2019-10-12 02:25:52
【问题描述】:

有 2 个原型单元。每个都需要不同的大小。所以我有单元格和单元格1。单元格应为 40,单元格应为 75。

我尝试使用 heightForRowAt - 发现这是在 cellForRowAt 之前调用的

我厌倦了在故事板上为每个单元格设置高度

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell", for: indexPath)
       let cell1 = tableView.dequeueReusableCell(withIdentifier: "starshipCell1", for: indexPath) as! SectionTableViewCell

       switch indexPath {
       case [4,0]:
           cell1.sectionLbl.text = "Armor".lowercased()
           cell1.detailTextLabel?.text = "Test"
           return cell1
       case [4,1]:
           cell.textLabel?.text = "Defensive Countermeasures"
           return cell
       case [4,2]:
           cell.textLabel?.text = "Shields"
           return cell
       case [11, 0]:
           cell.textLabel?.text = "Forward Arc"
           return cell
       case [11, 1]:
           cell.textLabel?.text = "Port Arc"
           return cell
       case [11, 2]:
           cell.textLabel?.text = "Starboard Arc"
           return cell
       case [11, 3]:
           cell.textLabel?.text = "Aft Arc"
           return cell
       case [11, 4]:
           cell.textLabel?.text = "Turret"
           return cell
       default:
           return cell
       }

      // return cell
   }

'试图为同一索引路径使多个单元格出列,这是不允许的。如果您确实需要比表格视图请求更多的单元格出列,请使用 -dequeueReusableCellWithIdentifier: 方法(没有索引路径)。单元标识符:starshipCell1,索引路径:{length = 2, path = 0 - 0}'

所以上面的工作完美。我只需要调整这些单元格的行高。现在代码不是 100% 完成的。以上所有案例都将更改并更新为cell1。错误仅在我使用 heightForRowAt 时出现

【问题讨论】:

  • 您是否在两个原型单元格中都使用自动布局?
  • 我还没有添加。我刚刚发现我可以有多个原型单元。所以我正在测试一个概念。现在我注意到在故事板中,如果我改变单元格本身的高度,那么在构建它时它不会改变它。但是,如果我选择表格并更改行高,它将应用所需的高度。这里的问题是我需要不同的高度。
  • 您可以在两个原型单元格中使用自动布局。并在 heightForRow 方法中使用 UITableView.automaticDimension 。你会得到 2 个不同高度的单元格。检查这个thomashanning.com/uitableview-automatic-row-height
  • 那个教程没有帮助。我没有将推送到第二行的文本。
  • 你想什么时候用cell,什么时候用cell1?

标签: swift uitableview height cell


【解决方案1】:

cellForRowAt 方法中,您只能出列并返回一个单元格。要在给定索引路径的情况下返回正确的,通常的做法更像是以下内容:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell
    if indexPath.row == 0 { // starshipCell only appears if it's the first row
        cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell", for: indexPath)
    } else { // Otherwise, we use starshipCell1
        cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell1", for: indexPath) as! SectionTableViewCell
    }

    // Set up the cell here

    return cell
}

这样,dequeueReusableCell 每个单元格只调用一次。 (错误)heightForRowAt 应该类似地使用。

【讨论】:

  • 更新了完整的 cellForRowAt
  • 问题仍然存在。您不能调用 dequeueReusableCell 然后在下一行再次调用它。您必须根据行号决定它将是什么单元格类型,并从执行的那个点开始坚持下去。从您现有的代码来看,我感觉您的 heightForRowAt 布局已经正确。
  • 我会再次告诉你,上面的工作完全符合我的需要。我有 2 个不同的原型单元。每个单元格都有自己的标识符,上面列出的 indexPath 使用第二次重用。我唯一可以处理高度变化的事情是当我选择了表格时。然后,我更改了行高,这两个单元格都发生了变化。如果我更改单元格的高度,它仍然是默认高度。
猜你喜欢
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多