【发布时间】:2019-07-17 23:32:44
【问题描述】:
我正在开发一个应用程序,它将显示通知,允许用户发布 cmets 并编辑通知并在通知上查看 cmets。
为此,我有 3 个原型单元格,分别称为“活动”、“控制”和“评论”单元格。我的前两个 Active 和 Control 在所有设备上的自动调整大小看起来和工作正常。然而,评论单元在 Plus 尺寸的 iOS 设备(不包括 iPad)上缩小到无法阅读的尺寸。
评论单元格在堆栈视图中仅包含 3 个大小和颜色略有不同的标签。这是在 Swift 4 上。
发生这种情况的设备列表: 6+, 6s+, 7+, 8+, X, Xs, Xs Max
上图显示了 iPhone XR 上的预期单元格和 Xs Max 上的意外单元格
我尝试编辑压缩和拥抱规则,尝试使用约束来强制高度(这在技术上有效,但它破坏了外观并发出警告)。
func numberOfSections(in tableView: UITableView) -> Int {
if activeEvents.count == 0 {
self.tableView.isHidden = true
self.activityIndicator.isHidden = false
self.activityLabel.isHidden = false
self.activityIndicator.startAnimating()
} else {
self.tableView.isHidden = false
self.activityIndicator.isHidden = true
self.activityLabel.isHidden = true
self.activityIndicator.stopAnimating()
}
return activeEvents.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if activeEvents[section].expanded {
return activeEvents[section].comments.count + 2
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let activeEvent = activeEvents[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "activeCell") as! ActiveCell
cell.selectionStyle = .none
cell.setCell(active: activeEvent)
return cell
} else if indexPath.row == 1 {
let activeEvent = activeEvents[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "controlCell") as! ControlCell
cell.setNoti(active: activeEvent)
cell.selectionStyle = .none
cell.delegate = self
cell.vc = self
return cell
} else {
let activeEvent = activeEvents[indexPath.section]
let activeComment = activeEvent.comments[indexPath.row - 2]
let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell") as! CommentCell
cell.setCell(comment: activeComment)
return cell
}
}
【问题讨论】:
-
你可以做的一件事是在你的单元格中添加一个stackView,然后在所说的stackView中添加你的文本标签,只需将所有内容固定到边缘并且不要在两个元素中设置高度约束,那应该相应地自动调整单元格的大小,我也没有在你的代码中看到它,但我假设你正在你的 didload() 中实现 tableView.rowHeight = UITableViewAutomaticDimension 对吗?
-
我实际上没有,但是,我的 3 个标签在堆栈视图中。对于没有在最初的问题中添加该内容,我深表歉意。此外,在我的 XCode 上找不到自动维度,这是 swift 5 的新功能吗?我目前正在使用 swift 4。再次道歉很长时间的潜伏者第一次实际回答问题我将编辑问题以反映这一点。 @SamuelChavez
-
@HunterA。没有自动维度,这对 swift 5 来说并不新鲜,你应该能够实现它,只需在代码中将“tableView”更改为 tableView 的实际名称,这应该可以工作,除此之外你只需要检查你的约束,正如我所说,要使自动尺寸起作用,您不需要在单元格上设置高度限制,这是一个您会发现有用的链接raywenderlich.com/8549-self-sizing-table-view-cells
-
我添加了 tableView.estimatedRowheight 和 tableView.rowHeight = UITableView.automaticDimension 并从情节提要的单元格中删除了行高。
标签: swift xcode uitableview