ios - 具有自动布局约束的 UITableViewCell 子类大小不正确

标签 ios objective-c uitableview ios8

我正在尝试以编程方式创建一个自定义 UITableViewCell,没有 XIB,它由一个使用自动布局指定单元格高度的 UILabel 组成。我已经为单元格的 contentView 提供了这个标签的前导、尾随、顶部和底部约束,但这些约束不会影响表格 View 单元格的高度。标签全部堆叠在一起,没有填充,表格 View 分隔线也不与标签对齐。这里有什么问题?

enter image description here

自定义单元格:

@interface TransactionTableViewCell ()

@property (nonatomic, assign) BOOL didUpdateConstraints;

@end


@implementation TransactionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.translatesAutoresizingMaskIntoConstraints = NO;

        self.transactionPriceLabel = [[UILabel alloc] init];
        self.transactionPriceLabel.backgroundColor = [UIColor yellowColor];
        self.transactionPriceLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:self.transactionPriceLabel];
    }

    [self setNeedsUpdateConstraints]; //had to add this otherwise updateConstraints isn't called for some reason

    return self;

}

- (void)updateConstraints {
    if (!self.didUpdateConstraints) {
        self.didUpdateConstraints = YES;

        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeTrailing
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeTrailing
                                                                    multiplier:1
                                                                      constant:-15]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeTop
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeTop
                                                                    multiplier:1
                                                                      constant:15]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeBottom
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeBottom
                                                                    multiplier:1
                                                                      constant:15]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeLeading
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeLeading
                                                                    multiplier:1
                                                                      constant:15]];
    }

    [super updateConstraints];
}

View Controller :

- (void)viewDidLoad {
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 44.0;
    [self.view addSubview:tableView];

    tableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeLeading
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeLeading
                                                    multiplier:1
                                                      constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeTop
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeTop
                                                    multiplier:1
                                                      constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeBottom
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeBottom
                                                    multiplier:1
                                                      constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeTrailingMargin
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeTrailingMargin
                                                    multiplier:1
                                                      constant:0]];

    [tableView registerClass:[TransactionTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.transactionPriceLabel.text = @"label text";
    cell.transactionPriceLabel.textColor = [UIColor redColor];

    return cell;
}

最佳答案

标签底部到 contentView 约束底部的常量应该是 -15,而不是 15。

关于ios - 具有自动布局约束的 UITableViewCell 子类大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388845/

相关文章:

iphone - 在 iPhone 的 MapKit 上查找两点之间的路径/路线

objective-c - 嵌套 block 和对自身的引用

ios - 如何在 iOS 上将两个标签依次放入表格 View 单元格中?

iphone - UISearchbar取消按钮自定义图像

ios - 用户好友列表tableview为空解析查询未返回任何数据

ios - 用图像填充 UICollectionView 返回 nil : Swift 2

ios - iOS 8 Swift 2.2 中的 Uber SSO 登录问题

ios - PresentViewController dismiss 不会返回到之前的 viewController

ios - 重写 NSMutableUrlRequest 静态初始化程序?

ios - 如何在自定义 UITableViewCell 按钮和第二个 View 之间创建一个 segue