contentView not indenting in iOS 6 UITableViewCell prototype cell

后端 未结 6 2052
醉酒成梦
醉酒成梦 2020-12-02 05:35

I am configuring a custom UITableViewCell using a prototype cell in a Storyboard. However, all the UILabels (and other UI elements) do not seem to

6条回答
  •  情歌与酒
    2020-12-02 06:07

    Here is a subclass, based on other answers ideas, I'm going to base my custom cells on:

    @interface FixedTableViewCell ()
    
    - (void)initFixedTableViewCell;
    
    @end
    
    @interface FixedTableViewCell : UITableViewCell
    
    @end
    
    @implementation FixedTableViewCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        if (nil != (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
            [self initFixedTableViewCell];
        }
        return self;
    }
    
    - (void)awakeFromNib {
        [super awakeFromNib];
    
        [self initFixedTableViewCell];
    }
    
    - (void)initFixedTableViewCell {
        for (NSInteger i = self.constraints.count - 1; i >= 0; i--) {
            NSLayoutConstraint *constraint = [self.constraints objectAtIndex:i];
    
            id firstItem = constraint.firstItem;
            id secondItem = constraint.secondItem;
    
            BOOL shouldMoveToContentView = YES;
    
            if ([firstItem isDescendantOfView:self.contentView]) {
                if (NO == [secondItem isDescendantOfView:self.contentView]) {
                    secondItem = self.contentView;
                }
            }
            else if ([secondItem isDescendantOfView:self.contentView]) {
                if (NO == [firstItem isDescendantOfView:self.contentView]) {
                    firstItem = self.contentView;
                }
            }
            else {
                shouldMoveToContentView = NO;
            }
    
            if (shouldMoveToContentView) {
                [self removeConstraint:constraint];
                NSLayoutConstraint *contentViewConstraint = [NSLayoutConstraint constraintWithItem:firstItem
                                                                                         attribute:constraint.firstAttribute
                                                                                         relatedBy:constraint.relation
                                                                                            toItem:secondItem
                                                                                         attribute:constraint.secondAttribute
                                                                                        multiplier:constraint.multiplier
                                                                                          constant:constraint.constant];
                [self.contentView addConstraint:contentViewConstraint];
            }
        }
    }
    
    @end
    

提交回复
热议问题