Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableViewCell和UITableView之间添加NSLayoutControint_Ios_Swift_Uitableview_Nslayoutconstraint - Fatal编程技术网

Ios 在UITableViewCell和UITableView之间添加NSLayoutControint

Ios 在UITableViewCell和UITableView之间添加NSLayoutControint,ios,swift,uitableview,nslayoutconstraint,Ios,Swift,Uitableview,Nslayoutconstraint,所以,我想添加一个约束,强制每个单元格的最大高度小于或等于UITableView高度的2/3 我怎样才能做到这一点 在func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell中添加约束不是一个选项,因为该单元格不在UITableView中,所以它抱怨它们没有公共超级视图 那么基本上,我如何将我的UITableViewCells和我的UITableView与nslayoutcont

所以,我想添加一个约束,强制每个单元格的最大高度小于或等于
UITableView
高度的2/3

我怎样才能做到这一点

func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell
中添加约束不是一个选项,因为该单元格不在
UITableView
中,所以它抱怨它们没有公共超级视图


那么基本上,我如何将我的
UITableViewCell
s和我的
UITableView
nslayoutcontain
s联系起来呢?

你不能这样做。您可以做的是获取tableview的高度,然后在单元格上添加该数量的高度约束。因此,在interface builder中,为单元格添加一个高度约束,并将其设置为一个方便的值(只是为了查看表中的内容,它将从代码中更新)。然后在代码中为该约束添加引用。然后在“更新约束的值”中:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellidentifier", forIndexPath: indexPath) as! YourCustomCell
    cell.heightConstraint.constant =  2.0 * tableview.frame.size.height / 3.0
}
如果您使用的是动态大小单元格,则会出现这种情况。否则,您可以直接重写:
func tableView(\utableview:UITableView,

HeightForRowatineXpath indexPath:NSIndexPath)->CGFloat
并返回所需的大小。

是否需要在此处使用约束,是否可以从HeightForRowatineXpath中计算单元格高度?e、 g.return tableView.frame.size.height*2/3没有我想要的那么优雅,但这是个好主意。在任何情况下,它都不起作用,因为当时
cell.heightcontain
nil
。它不应该是。这只是一个普通的渠道。它的工作方式应与标签或视图插座的工作方式相同。确保使用正确的单元,并且约束处于活动状态。如果您停用它,如果您不将其指定为
strong
,它将被解除分配。这没有意义,为什么在将active设置为false时它会被解除分配?因为当您设置
。active=false
时它实际上已从布局中删除,因此不再有对它的强引用。我将尝试这样做。苹果的设计真糟糕。。。在任何情况下,我确实设法以另一种方式保留约束,它确实解决了这个问题,但当我将active设置为false时,它会打断其他单元格,就好像约束没有被停用一样。