OC-AutoLayout system UITableViewCell

之前计算自定义返回cell 的高度 ,现在看来实在太费事,也太坑爹。直接上代码吧


step:创建一个cell

@interface YYDynamicCell_system : UITableViewCell
@property(nonatomic,strong) id           object;
@end
@implementation YYDynamicCell_system

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier]) {
        self.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
        self.textLabel.numberOfLines = 0;
        [self.textLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        //
        self.detailTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
        self.detailTextLabel.numberOfLines = 0;
        self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        [self.detailTextLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addConstraintsToContentview];
    }
    return self;
}
-(void)addConstraintsToContentview{
    UIView *_conView = self.contentView;
    UIView *_textLabel = self.textLabel;
    UIView *_detailLabel = self.detailTextLabel;

    NSDictionary *viewDict = NSDictionaryOfVariableBindings(_conView,_textLabel,_detailLabel);
    NSDictionary *metrics = @{@"hPadding":@15,@"vPadding":@8};
    
    NSMutableArray *contraints = [NSMutableArray array];
    
    //method 01
//    [contraints addObject:[NSLayoutConstraint constraintWithItem:_textLabel attribute:NSLayoutAttributeFirstBaseline relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:30]];
//    [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_textLabel]-vPadding-[_detailLabel]" options:0 metrics:metrics views:viewDict]];
//
//    //
//    [contraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:_detailLabel attribute:NSLayoutAttributeLastBaseline multiplier:1.0 constant:10]];
//    //这个不用
////    [contraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:1 constant:44]];
//    //
////    [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_textLabel(_detailLabel)]-hPadding-|" options:0 metrics:metrics views:viewDict]];
//    [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_textLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]];
//
//        [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_detailLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]];
//    [self.contentView addConstraints:contraints];
    [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vPadding-[_textLabel]-vPadding-[_detailLabel]-vPadding-|" options:0 metrics:metrics views:viewDict]];
    
    [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_textLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]];
    
    [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_detailLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]];
    [self.contentView addConstraints:contraints];
    
}
-(void)setObject:(id)object{
    _object = object;
    self.textLabel.text = [object objectForKey:@"title"];
    self.detailTextLabel.text = [object objectForKey:@"detail"];

}
-(void)layoutSubviews{
    [super layoutSubviews];
    //preferredMaxLayoutWidth 这个是必须要添加的,否则可能会导致显示不正常
    self.textLabel.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20;
    self.detailTextLabel.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20;

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
@end


step2:在vc里面创建一个tableView,代理不用实现

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath


    _tv = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tv];
    _tv.dataSource = self;
    _tv.delegate = self;
    [_tv registerClass:[YYDynamicCell_system class] forCellReuseIdentifier:CellIdentifier];
// it is important
    _tv.rowHeight = UITableViewAutomaticDimension;



其他应该就很好做了吧。。。ok




你可能感兴趣的:(IOS,Objective-C,UITableViewCell,auto-layout)