【问题标题】:iOS Update TableViewCell ConstraintiOS 更新 TableViewCell 约束
【发布时间】:2015-08-13 03:47:48
【问题描述】:

我在处理TableViewCell 中的约束时遇到问题。我在 cell 中有一个 imageview,我想更新图像的约束。例如,宽度约束的默认值为120。如果imageUrl不为空,则宽度应为120,否则为0宽度。

我使用下面的代码将其更改为 0,它运行良好。在我滚动 TableView 后,由于约束将其锁定在 0 宽度,我的大部分图像都丢失了。我想将它更新回 120 宽度,但它似乎不起作用。任何帮助表示赞赏。

    CGFloat cellImageWidth = 120.0;

    NSDictionary *mainDictionary = @{@"cellImage":cellImage};
    NSArray *zeroWidth = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[cellImage(%f)]", 0.0]
                                                                             options:0
                                                                             metrics:nil
                                                                               views:mainDictionary];

    NSArray *normalWidth = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[cellImage(%f)]", cellImageWidth]
                                                                 options:0
                                                                 metrics:nil
                                                                   views:mainDictionary];


    if(![[imgArr objectAtIndex:indexPath.row] isEqualToString:@""]){
        [cellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", imageInbox, [imgArr objectAtIndex:indexPath.row]]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


        cellImage.hidden = NO;

        [cellImage removeConstraints:zeroWidth];
        [cellImage addConstraints:normalWidth];
        [cellImage updateConstraints];

    }else{
        cellImage.hidden = YES;

        [cellImage removeConstraints:normalWidth];
        [cellImage addConstraints:zeroWidth];
        [cellImage updateConstraints];

    }

【问题讨论】:

    标签: ios uitableview autolayout constraints tableviewcell


    【解决方案1】:

    想一想:

    1. 为什么不更新使用的约束?设置约束一次,如果要更新更改 constant 属性。
    2. 您真的需要将约束更改为零吗?其他约束取决于它?
    3. 覆盖prepareForReuse。当准备重用 UITableViewCell 以显示 UITableView 中另一个对象的信息时调用此方法,这在发生滚动时偶然发生。解决方案很简单,只需将以下代码插入我的 UITableViewCell 子类:-(void) prepareForReuse { [self setNeedsUpdateConstraints]; }

    【讨论】:

    • 1.我尝试使用常量属性。但是在 tableviewcell 中它不允许这种方法。这将导致编译期间出错。 2. 是的,其他约束取决于它。 3.这是否意味着我应该使用prepareForReuse方法创建一个自定义的tableviewcell?
    • 3. prepareForReuse 是 UITableViewCell 生命周期的一部分:developer.apple.com/library/ios/documentation/UIKit/Reference/…
    • 2.如果你得到一个约束的错误变化常数,你做错了。你有这个 UITableViewCell 的 XIB 或 Storboard 单元吗?如果是,请将您在 xib/storyboard 中设置的约束连接到代码中的插座,并修改其常量值。如果没有,你在哪里创建这个约束(方法)?
    【解决方案2】:

    尝试使用它。

    NSLayoutConstraint *myConstraint =[NSLayoutConstraint 
                     constraintWithItem:Your_Image
                     attribute:NSLayoutAttributeWidth
                     relatedBy:NSLayoutRelationGreaterThanOrEqual
                     toItem:nil
                     attribute:NSLayoutAttributeWidth
                     multiplier:1
                     constant:120];//120 is the width you want 
    

    【讨论】:

    • 我试过这个方法。遗憾的是结果是一样的。它没有更新回 120
    • 你可以尝试创建宽度约束的出口,然后将其常量设置为 120。
    • 看来xcode不允许将IBOutlet与tableviewCell的内容进行匹配。我不确定是否有解决方法。有什么想法吗?
    • 您必须从情节提要中添加约束。然后转到图像的宽度约束,然后拖放到您的 .h 文件中,它将创建宽度约束的出口。 & 然后将代码编写为 YOUR_OUTLET_NAME.constant=120
    • 是的。我做了和你说的一样的方法。在编译期间,该约束会发生错误。它不允许编译
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多