How to create a custom UITableViewCell programmatically using AutoLayout

后端 未结 2 1270
轮回少年
轮回少年 2020-12-12 18:17

I\'m trying to implement a UITableView that will behave similarly to the timeline of a twitter client. Right now I\'m simply attempting to get two labels inside a UITableVie

2条回答
  •  不知归路
    2020-12-12 19:03

    There are several things wrong with your code. First, I think you'll find, if you do some logging, that updateConstraints is never called. I would put all the code in the init method. Also, there are several things wrong in your constraints. The constraint where you set the height to 44 is not needed since you already have the labels pinned to the to and bottom of the cell. I don't know what you're trying to do with that last one, it looks like that would make the nameLabel 1 point wide. Also, you shouldn't set the translatesAutoresizingMaskIntoConstraints to NO for the content view, that causes weird effects. So this is the code I think you want:

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            reuseID = reuseIdentifier;
    
            nameLabel = [[UILabel alloc] init];
            [nameLabel setTextColor:[UIColor blackColor]];
            [nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
            [nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
            [nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
            [self.contentView addSubview:nameLabel];
    
            mainLabel = [[UILabel alloc] init];
            [mainLabel setTextColor:[UIColor blackColor]];
            [mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]];
            [mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
            [mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
            [self.contentView addSubview:mainLabel];
    
            NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel);
            if (reuseID == kCellIDTitle) {
                NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
                                                                               options: 0
                                                                               metrics:nil
                                                                                 views:views];
                [self.contentView addConstraints:constraints];
                constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel]|"
                                                                      options: 0
                                                                      metrics:nil
                                                                        views:views];
                [self.contentView addConstraints:constraints];
            }
            if (reuseID == kCellIDTitleMain) {
                NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:views];
                [self.contentView addConstraints:constraints];
    
                constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainLabel]|"
                                                                      options: 0
                                                                      metrics:nil
                                                                        views:views];
                [self.contentView addConstraints:constraints];
    
                constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel][mainLabel(==nameLabel)]|"
                                                                      options: 0
                                                                      metrics:nil
                                                                        views:views];
                [self.contentView addConstraints:constraints];
    
            }
        }
        return self;
    }
    

提交回复
热议问题