【发布时间】:2016-03-15 15:12:26
【问题描述】:
我在UITableView 中有多个原型单元。现在在其中一个单元格中,我有一个带有 top、'bottom'、leading 和 trailing 约束的 UILabel。我希望以编程方式修改 bottom 约束。如何在我的代码中评估此约束?
我在xib 中给了这个约束identifier,但是当我做lbl.constraints 时,我只得到以下约束的数组:
po lbl.constraints
<__NSArrayI 0x7f987fb81a60>(
<NSContentSizeLayoutConstraint:0x7f987faca450 H:[UILabel:0x7f98815f4660'Schedule Showing'(117)] Hug:251 CompressionResistance:750>,
<NSContentSizeLayoutConstraint:0x7f987faca4b0 V:[UILabel:0x7f98815f4660'Schedule Showing'(16.5)] Hug:251 CompressionResistance:750>
)
为什么没有显示尾随、前导、底部和顶部约束?我哪里错了?我该如何解决?
编辑(未反映约束更改)
-(IBAction)btnShowCtrl_click:(id)sender{
FormTableViewCell *cell = (FormTableViewCell *)[_tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:[[dictData valueForKey:@"FeatureList"] count]+4]];
if(cell.nlcTextField.priority == 999){
cell.nlcLabel .priority = 999;
cell.nlcTextField.priority = 500;
} else {
cell.nlcLabel .priority = 500;
cell.nlcTextField.priority = 999;
}
[UIView animateWithDuration:.5 animations:^{
// self.orangeView.alpha = 0;
[_tblView beginUpdates];
[_tblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:[[dictData valueForKey:@"FeatureList"] count]+4]] withRowAnimation:UITableViewRowAnimationNone];
[_tblView endUpdates];
[cell layoutIfNeeded];
}];
}
【问题讨论】:
-
将约束附加到 Interface Builder 中的 NSLayoutConstraint 出口。
-
约束在superview。但与其以编程方式遍历约束,不如为约束创建
@IBOutlet并将其用于修改目的要容易得多。 -
@Rob - 我有没有实现文件的原型单元。我只是通过标签使用单元格及其视图。因此无法在 ViewController 的 .h 文件中创建
@IBOutlet。 -
@z22 但是如果您创建 uitableviewcell 的子类并将其指定为原型单元的基类,则无需实现任何方法,但可以享受无缝插座(避免使用无论如何,Apple 建议不要使用标签)。它使您的代码更加清晰易读,超级简单,您不仅可以享受控件的输出,还可以享受约束。
-
@Rob 好吧,所以现在我创建了 UITableviewCell 的子类并在
viewDidLoad中使用了registerclass..。在cellForRowAtIndexpath我用过-strIdentifier = @"FormTableViewCell"; FormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier forIndexPath:indexPath];,应用程序崩溃了,我哪里弄错了。不太熟悉将registerClass与多个原型单元一起使用。
标签: ios objective-c uitableview autolayout