ios – Swift TableViewCell xib没有约束
我有两个TableView,当我设计原始的时候,我使用故事板中的原型单元设计它,使其可重用,我试图将其拉出到.xib并加载它.当它从cellID.xib加载时,它会在运行时丢失所有约束,并且所有约束都相互叠加.
TableViewController let cellIdentifier = "cellID" override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: cellIdentifier,bundle: nil),forCellReuseIdentifier: cellIdentifier) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 300 } 故事板中的原型单元(用于工作) 复制粘贴到XIB时的单元格 约束 XIB视图层次结构 解决方法
问题
在您的问题标题中,您询问了如何解决“Swift TableViewCell xib没有约束”的问题.此外,在赏金中,您指定答案应该是:
根据我的理解,你的问题有两个部分: >自定义TableViewCell xib没有约束的问题 对于问题#1,我使用了几乎所有与您在自定义单元格xib中的屏幕截图中显示的相同的约束,并且它有效.我将在下面详细解释. 对于问题#2,我发现约束不会丢失.您可能需要共享您的项目,以便其他人可以复制您遇到的问题.我将具有类似约束的Storyboard原型单元复制/粘贴到xib上,并且我没有任何约束问题.所以我可以确认复制/粘贴功能是否有效. 解决方案演示 我创建了以下演示来测试您的问题.请参阅下面的屏幕截图. tableView包含六个单元格.第一个和第二个是相同的,并具有重用标识符MyCell.第3和第4个是相同的,并具有重用标识符MyCopyPasteCell.第5和第6个是相同的并且具有重用标识符MyPrototypeCell. MyCell存在于xib中,并且几乎使用了屏幕截图中显示的所有相同约束.如您所见,没有约束问题. MyCopyPasteCell使用类似的约束,并从Storyboard原型复制/粘贴到xib. MyPrototypeCell是复制的原始原型.即使将原型复制到xib,最后四个单元看起来完全相同.从原型到xib的约束没有问题. 约束 在下面的代码中,我列出了您在ContentView的屏幕截图中显示的所有约束. (请注意,我还实现了height = 20,aspect = 1:1和height = 75约束,尽管它们未在下面列出.)由于我没有使用它们,因此注释掉了两个约束.我还添加了一个约束来替换另一个未使用的约束. // bottomMargin = Profile Image View.bottom + 188.5 bottomMargin = Profile Image.bottom + 17 Profile Image View.top = Username Label.top Profile Image View.leading = leadingMargin + 2 Profile Image View.top = topMargin + 20 Username Label.leading = Content Text View.leading // Username Label.top = topMargin + 20 Username Label.trailing = Content Text View.trailing Username Label.leading = Profile Image View.trailing + 15 trailingMargin = Username Label.trailing + 10 Content Text View.top = Username Label.bottom + 5 Content Text View.leading = leadingMargin + 92 bottomMargin = Content Text View.bottom + 20 第一个注释约束// bottomMargin = Profile Image View.bottom 188.5没有意义,因为它会将图像的底部与单元格的底部分开188.5.这也与Storyboard(用于工作)截图中的Prototype单元格不匹配.我将它替换为bottomMargin = Profile Image.bottom 17,它将188.5替换为17. 第二个注释约束// Username Label.top = topMargin 20(将username和topMargin分隔为20)在技术上可以工作.但是,它的功能对于Profile Image.top = topMargin 20(将Profile Image和topMargin分隔为20)和Profile Image View.top =用户名Label.top(将Profile Image和Username设置为相同的顶部分隔,即通过20). MyTableViewController 以下是我的视图控制器.基本上我创建了三个部分,每个部分有两个单元格/行. MyCell和MyCopyPasteTableViewCell来自xib,并在viewDidLoad()中注册. MyPrototypeCell来自Storyboard,未在viewDidLoad()中注册. // MyTableViewController.swift import UIKit class MyTableViewController: UITableViewController { let myCell = "MyCell" let myCopyPasteCell = "MyCopyPasteTableViewCell" let myPrototypeCell = "MyPrototypeCell" override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: myCell,forCellReuseIdentifier: myCell) tableView.register(UINib(nibName: myCopyPasteCell,forCellReuseIdentifier: myCopyPasteCell) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 300 } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 3 } override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return 2 } override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: myCell,for: indexPath) return cell } else if indexPath.section == 1 { let cell = tableView.dequeueReusableCell(withIdentifier: myCopyPasteCell,for: indexPath) return cell } else { let cell = tableView.dequeueReusableCell(withIdentifier: myPrototypeCell,for: indexPath) return cell } } } Github链接 https://github.com/starkindustries/TableViewCellConstraintsTest (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |