我有两个 TableView,当我设计原件时,我使用 Storyboard 中的原型(prototype)单元设计了它,为了使其可重用,我试图将它拉出到 .xib 并加载它。但是,当它从 cellID.xib 加载时,它会在运行时失去所有约束,并且所有内容都层叠在一起。
let cellIdentifier = "cellID"
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
}
最佳答案
在您的问题标题中,您询问了如何解决“Swift TableViewCell xib 没有约束”的问题。此外,在赏金中,您指定答案应该是:
An adequate answer showing why constraints are lost when moved to a xib, when they exist in the xib, and how to address this problem.
据我了解,您的问题分为两部分:
对于问题 #1,我使用了几乎所有与您在自定义单元格 xib 的屏幕截图中显示的相同的约束,并且它有效。我将在下面更详细地解释。
对于问题 #2,我发现约束并没有丢失。您可能需要共享您的项目,以便其他人可以复制您遇到的问题。我将具有类似约束的 Storyboard 原型(prototype)单元复制/粘贴到 xib,并且我没有遇到任何约束问题。所以我可以确认复制/粘贴功能确实有效。
我创建了以下演示来测试您的问题。请参阅下面的屏幕截图。 tableView 包含六个单元格。第一个和第二个是相同的,并且具有重用标识符 MyCell。第 3 个和第 4 个相同,并且具有重用标识符 MyCopyPasteCell。第 5 个和第 6 个相同,并且具有重用标识符 MyPrototypeCell。
MyCell 存在于 xib 中并使用几乎所有与您在屏幕截图中显示的相同的约束。如您所见,不存在约束问题。 MyCopyPasteCell 使用类似的约束,并从 Storyboard 原型(prototype)复制/粘贴到 xib。 MyPrototypeCell 是复制的原始原型(prototype)。这最后四个单元格看起来完全一样,即使原型(prototype)被复制到 xib。约束从原型(prototype)转移到 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(用 20 分隔 username 和 topMargin)在技术上是可行的。但是,它的功能对于 Profile Image.top = topMargin + 20(将 Profile Image 和 topMargin 相隔 20)和 Profile Image View.top = Username Label.top 是多余的(它将个人资料图像和用户名设置为相同的顶部分隔,即 20)。
以下是我的 View Controller 。基本上我创建了三个部分,每个部分有两个单元格/行。 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, bundle: nil), forCellReuseIdentifier: myCell)
tableView.register(UINib(nibName: myCopyPasteCell, bundle: nil), 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
}
}
}
https://github.com/starkindustries/TableViewCellConstraintsTest
关于ios - Swift TableViewCell xib 没有约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43521023/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里