class Cell:UITableViewCell {
var myImage:UIImageView = UIImageView()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
myImage.setTranslatesAutoresizingMaskIntoConstraints(false)
time.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(userImage)
contentView.addSubview(time)
let viewsDict = ["contentView":contentView,"myImage":myImage,"time":time]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage(50)]-[time]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[myImage(50)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
}
我想将时间附加到 subview 的右侧,即 contentView,但它在 myImage 旁边:
即使在横向模式下,contentView 也不会到达所有屏幕:
我试过 contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)),但我收到一个错误:
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70] (Names: '|':myApp.Cell:0x79e58f00'cell' )>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-01-13 20:05:11.546 myApp[6117:775684] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70] (Names: '|':myApp.Cell:0x79e58f00'cell' )>
Container hierarchy:
<UITableViewCellContentView: 0x79e59f70; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x79e5a8a0>; layer = <CALayer: 0x79e5a140>>
| <UILabel: 0x79e59520; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x79e595e0>>
| <UIImageView: 0x79e59750; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x79e597d0>>
View not found in container hierarchy: <myApp.Cell: 0x79e58f00; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x79e59b50>>
That view's superview: NO SUPERVIEW
2015-01-13 20:05:11.857 myApp[6117:775684] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x79e5d7c0 H:|-(0)-[UITableViewCellContentView:0x79e59f70] (Names: '|':myApp.Cell:0x79e58f00'cell' )> view:<UITableViewCellContentView: 0x79e59f70; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x79e5a8a0>; layer = <CALayer: 0x79e5a140>>'
更新:我在向 tableView 添加约束时修复了行的宽度,但标签 time 仍然不在 contentView 的右侧。有什么建议么?看起来 contentView 不像 tableView 宽度那样长。我不知道……
最佳答案
尝试在父 View 上添加 contentView 约束:
contentView.superview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
您可能还需要调用它以使单元格正确呈现:
self.setTranslatesAutoresizingMaskIntoConstraints(false)
关于ios - Swift - UITableViewCell 类 contentView 以编程方式约束没有宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27928614/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我好像记得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返回它复制的字节数,但是当我还没有下
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示: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