【问题标题】:UITableViewCell Size is not expanding at runtime .?UITableViewCell 大小在运行时没有扩大。?
【发布时间】:2018-04-12 09:56:36
【问题描述】:

here is a storyboard image. i have take a dynamic tableviewcell 我尝试过拖放UITableViewCell并从情节提要更改行高,它无法在运行时扩展单元格高度。

Xcode 9.0.1Swift 4 中,我遇到了关于单元格高度在运行时没有增加的问题。所以,请帮助我解决这个问题。

【问题讨论】:

  • 您使用的是静态单元格还是动态单元格?
  • 使用表格单元约束布局和表格视图委托/数据源的来源展示您的故事板设计
  • @MartinMuldoon - 动态单元格
  • @Krunal- Storyboard design screen short :-i.stack.imgur.com/Plku3.png

标签: ios swift uitableview tableview row-height


【解决方案1】:

要为行高和估计行高设置自动尺寸,请确保按照以下步骤制作,自动尺寸对单元格/行高布局有效。

  • 分配和实现 dataSource 和委托
  • UITableViewAutomaticDimension 分配给rowHeight 和estimatedRowHeight
  • 实现委托/数据源方法(即heightForRowAt并返回一个值UITableViewAutomaticDimension给它)

-

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

对于 UITableviewCell 中的标签实例

  • 设置行数=0(&换行模式=截尾)
  • 设置与其父视图/单元格容器相关的所有约束(上、下、左右)。
  • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

【讨论】:

  • 谢谢。它正在工作!
【解决方案2】:

试试这个:

1. 实现UITableViewDelegate 方法:

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
    return 200
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension
}

2. 将正确的constraints 应用于您的UITableViewCell,以便它可以正确计算自己的height。必须出现正确的UITableViewCell UI。 autolayout 中的任何问题都可能导致您的单元 UI 以意外方式运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    相关资源
    最近更新 更多