- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UITableViewController
,它是 3 种不同单元类型的组合。当将新单元格推送到 TableView 并调用重新加载数据时,我遇到了一个奇怪的错误。
此时,添加了 2 个新单元格并调用了 reloadData
。
正如您所看到的,突出显示的单元格现在具有非常不正确的约束,实际上在模拟器中上下滚动会导致其他单元格损坏。就好像问题随机地从一个单元跳到另一个单元一样。
控制台确实输出了一个调试错误,但是我不确定如何对此进行正面或反面
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60000201ccd0 UIImageView:0x7ffb7bd4d740.trailing == UILayoutGuide:0x600003a79ea0'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000201c3c0 UIView:0x7ffb7bd4d970.leading == UILayoutGuide:0x600003a79ea0'UIViewLayoutMarginsGuide'.leading + 15 (active)>",
"<NSLayoutConstraint:0x6000020194a0 H:[UIImageView:0x7ffb7bd4d740]-(15)-[UIView:0x7ffb7bd4d970] (active)>",
"<NSLayoutConstraint:0x600002012d50 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ffb7bd53580.width == 375 (active)>",
"<NSLayoutConstraint:0x60000201d7c0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600003a79ea0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x7ffb7bd53580 )>",
"<NSLayoutConstraint:0x60000201c690 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600003a79ea0'UIViewLayoutMarginsGuide']-(16)-|(LTR) (active, names: '|':UITableViewCellContentView:0x7ffb7bd53580 )>"
)
我以编程方式设置约束,此 View 中没有 Storyboard或 Nib 。
我非常感谢有关如何解决此问题的指导或指导。
private let chatCellId = "chatCellId"
private let mediaCellId = "mediaCellId"
private let ctaCellId = "ctaCellId"
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ChatMessageCell.self, forCellReuseIdentifier: chatCellId)
tableView.register(MediaMessageCell.self, forCellReuseIdentifier: mediaCellId)
tableView.register(CTAMessageCell.self, forCellReuseIdentifier: ctaCellId)
tableView.tableFooterView = UIView()
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.clear
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
viewModel.reloadData = { [unowned self] in
DispatchQueue.main.async {
self.reloadTableView()
}
}
viewModel.fetchBotResponse(byKey: "welcome")
vc.delegate = self
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellType = viewModel.messages[indexPath.row].type
if cellType == .media {
let cell = tableView.dequeueReusableCell(withIdentifier: mediaCellId, for: indexPath) as! MediaMessageCell
cell.content = viewModel.messages[indexPath.row]
return cell
} else if cellType == .callToAction {
let cell = tableView.dequeueReusableCell(withIdentifier: ctaCellId, for: indexPath) as! CTAMessageCell
cell.content = viewModel.messages[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: chatCellId, for: indexPath) as! ChatMessageCell
cell.content = viewModel.messages[indexPath.row]
return cell
}
}
聊天消息单元格
扩展 ChatMessageCell {
fileprivate func anchorViews() -> Void {
let marginGuide = contentView.layoutMarginsGuide
[avatar, messageBackground].forEach { addSubview($0) }
messageBackground.insertSubview(messageText, at: 0)
if content?.origin == .system {
let avatarInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
let avatarSize = CGSize(width: 35, height: 35)
avatar.image = #imageLiteral(resourceName: "large_bot_head")
avatar.anchor(top: topAnchor, leading: leadingAnchor, bottom: nil, trailing: nil, padding: avatarInsets, size: avatarSize)
let messageBackgroundInsets = UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 0)
messageBackground.anchor(top: topAnchor, leading: avatar.trailingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: messageBackgroundInsets)
let messageTextInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
messageText.anchor(top: messageBackground.topAnchor, leading: messageBackground.leadingAnchor, bottom: messageBackground.bottomAnchor, trailing: messageBackground.trailingAnchor, padding: messageTextInsets)
} else {
avatar.image = UIImage.from(color: UIColor.hexStringToUIColor(hex: "00f5ff"))
messageBackground.backgroundColor = UIColor.hexStringToUIColor(hex: "00f5ff")
messageBackground.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
let avatarInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
let avatarSize = CGSize(width: 35, height: 35)
avatar.image = #imageLiteral(resourceName: "large_bot_head")
avatar.anchor(top: marginGuide.topAnchor, leading: nil, bottom: nil, trailing: marginGuide.trailingAnchor, padding: avatarInsets, size: avatarSize)
let messageBackgroundInsets = UIEdgeInsets(top: 15, left: 15, bottom: 0, right: 15)
messageBackground.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: avatar.leadingAnchor, padding: messageBackgroundInsets)
let messageTextInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
messageText.anchor(top: messageBackground.topAnchor, leading: messageBackground.leadingAnchor, bottom: messageBackground.bottomAnchor, trailing: messageBackground.trailingAnchor, padding: messageTextInsets)
}
}
}
CTA消息单元格
extension CTAMessageCell {
fileprivate func anchorViews() -> Void {
let marginGuide = contentView.layoutMarginsGuide
[avatar, messageBackground].forEach { contentView.addSubview($0) }
messageBackground.insertSubview(messageText, at: 0)
messageBackground.insertSubview(buttonStackView, at: 0)
let avatarInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
let avatarSize = CGSize(width: 35, height: 35)
avatar.image = #imageLiteral(resourceName: "large_bot_head")
avatar.anchor(top: marginGuide.topAnchor, leading: marginGuide.leadingAnchor, bottom: nil, trailing: nil, padding: avatarInsets, size: avatarSize)
let messageBackgroundInsets = UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 0)
messageBackground.anchor(top: marginGuide.topAnchor, leading: avatar.trailingAnchor, bottom: marginGuide.bottomAnchor, trailing: marginGuide.trailingAnchor, padding: messageBackgroundInsets)
let messageTextInsets = UIEdgeInsets(top: 15, left: 15, bottom: 0, right: 15)
messageText.anchor(top: messageBackground.topAnchor, leading: messageBackground.leadingAnchor, bottom: nil, trailing: messageBackground.trailingAnchor, padding: messageTextInsets)
let buttonStackViewInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
buttonStackView.anchor(top: messageText.bottomAnchor, leading: messageBackground.leadingAnchor, bottom: messageBackground.bottomAnchor, trailing: messageBackground.trailingAnchor, padding: buttonStackViewInsets)
}
}
我使用以下扩展进行锚定
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
translatesAutoresizingMaskIntoConstraints = false
var anchoredConstraints = AnchoredConstraints()
if let top = top {
anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
}
if let leading = leading {
anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
}
if let bottom = bottom {
anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
}
if let trailing = trailing {
anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
}
if size.width != 0 {
anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
}
if size.height != 0 {
anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
}
[anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
return anchoredConstraints
}
最佳答案
这三个约束是冲突的:
"<NSLayoutConstraint:0x60000201ccd0 UIImageView:0x7ffb7bd4d740.trailing ==
UILayoutGuide:0x600003a79ea0'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000201c3c0 UIView:0x7ffb7bd4d970.leading ==
UILayoutGuide:0x600003a79ea0'UIViewLayoutMarginsGuide'.leading + 15 (active)>",
"<NSLayoutConstraint:0x6000020194a0 H:[UIImageView:0x7ffb7bd4d740]-(15)-
[UIView:0x7ffb7bd4d970] (active)>",
第二个约束和第三个约束将 View 的前缘固定到不同的位置。但从您发布的代码中尚不清楚为什么会出现这种情况。
我猜单元的可重用性已经让你陷入困境了。考虑上面的 ChatMessageCell。如果重复使用同一个单元格,并且 content?.origin == .system
第一次为 true,但第二次不是,那么您最终将受到来自两条路径的约束,这将导致约束问题 - 除非您删除或停用不需要的约束(例如在 prepareForReuse
中)。
另外:https://www.wtfautolayout.com/使 Apple 的约束日志可视化变得更加容易。
关于ios - 添加新单元格时 UITableViewCell 约束被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53712364/
我是 Swift 开发新手,正在学习许多在线教程。这些教程中的大多数都引用了 Xcode 上的旧版本,并且代码会导致错误。谁能解释为什么下面的代码会产生“UITableViewCell”?无法转换为
我在我的单元格中使用 AutoLayout,但是,当单元格内有大文本并且单元格展开时,单元格似乎与下面的单元格重叠。图像显示了这是如何重叠单元格的: UITableViewCell:
我的函数接受类型为 (cell: UITableViewCell, item: AnyObject) -> () 的函数作为参数。 我的问题是,当我尝试将具有 UITableViewCell 的子类
我正在尝试做一些相当简单的事情,但我看不到最好的方法。我有一个带有两个单元格的 uitableview,第一个在 contextview 中有一个 uitextfield,输入 View 设置为 ui
我在使用 Swift 时遇到了一些挑战。 我有一个带有原型(prototype)单元的 UITableView。有3个单元格前两个包含用户填写的文本字段。第三个包含一个提交按钮。 非常简单! 我的问题
我是 Objective C 的新手。我有一个问题。 我使用自定义的tableviewcell。我正确地使用 JSON 列出了数据。 我想将点击的数据传递到新 View 的自定义表格 View 。 所
我有两个单元格,比方说我的应用程序中的单元格 A 和单元格 B。它们都有自己的类,class CellA : UITableViewCell 和 class CellB : UITableViewCe
我有一个 UIViewController,它根据屏幕上的所需选项实例化一些 UITableViewCell。这些选项之一是“说明”,我创建了一个 .XIB 文件来在屏幕上显示说明。但是,在此屏幕上,
我正在尝试创建一个 tableview,其中有一个 uitableviewcell(cell1) 的子类,其中包含一个按钮等。当在 cell1 中单击该按钮时,该按钮应该在其正下方添加和删除 uita
在使用 UITableView 编码时,我遇到了这个我无法理解的错误。我的表格有 3 行,每行设置 50 像素,但第二行除外,我将其设置为 500 像素,它填满整个屏幕。当我向下滚动到底部时,问题出现
UITableViewCell 反射(reflect)两种不同的状态:突出显示和选定。 对我来说,它们听起来相同,那么到底有什么区别? 最佳答案 突出显示发生在触地时。 选定发生在触摸时,然后调用di
我刚刚添加了屏幕截图。它是定制单元吗?正如我所见,“ map ”是右对齐的,“1800”是左对齐的。我该如何创建它? 最佳答案 它确实看起来像一个自定义单元格。因为 UITableViewCell 继
我不太了解 tableview 的整体行为: 我有一个带有我在 Storyboard中定义的动态单元格(reuseIdentifier:单元格)的表格 View 。除此之外,我还有两个使用两个 nib
我有一个带有以下代码的 UITableViewCell,其中用户单击 UITableViewCell 上的“保存”按钮。单元格中的图像已保存,“保存”按钮动画消失,但单元格仍然存在。 -(UIT
我有一个UITableView,每个单元格都包含一个UIImageView 和一个UIButton。点击按钮后我启动相机,我正在尝试更新该单元格索引路径中的 UIImageView 。到目前为止,这是
有谁知道如何在进入编辑模式时从分组的 UITableView 中隐藏多个单元格?当退出编辑模式时,我希望这些行以动画效果隐藏,就像在“联系人”应用程序中看到的那样。 如您所知,在联系人编辑模式下,行数
我在 UITableViewCell 中有一个 subview ,其中包含一个 UILabel 和一个 UIButton。当我按下按钮时,它出于某种原因取消选择 UITableViewCell。 有没
我一直在经历在 UItableViewCell 中设置 UIDatePicker 的过程,当选择其上方的 UITableViewCell 时会显示该 UIDatePicker,然后在选择 UITabl
我必须集成从代码中以编程方式生成的 UITableViewCell,如下所示: UITableViewCell *newCell = [[UITableViewCell alloc] initWith
我现在正在学习 iOS 开发中的 UITableView 类,我想在导航到多个 UITableViewCells 的部分中实现点击一个 UITableViewCell,该附件类型用于复选标记,我应该怎
我是一名优秀的程序员,十分优秀!