我需要设计自定义单元格,如下面的nslayoutconstraints,通过程序化的方式使用可视化格式语言以及constraintswithitem。任何帮助都会很感激。
`/ *初始化并指定产品名称* / productName = [[UILabel alloc] init]; productName.font = [UIFont fontWithName:@" HelveticaNeue"尺寸:17.0f]; self.productName.lineBreakMode = NSLineBreakByWordWrapping; self.productName.numberOfLines = 0; productName.translatesAutoresizingMaskIntoConstraints = NO; [self.contentView addSubview:productName];
/* Intialize and assign Product Option */
productOption=[[UILabel alloc]init];
productOption.translatesAutoresizingMaskIntoConstraints=NO;
productOption.font=[UIFont fontWithName:@"HelveticaNeue" size:13.0f];
productOption.textColor=[UIColor colorWithRed:128/255.0f green:128/255.0f blue:128/255.0f alpha:1.0f]; /* 808080 */
[self.contentView addSubview:productOption];
/* Intialize and assign Product Image */
productImageView=[[UIImageView alloc]init];
productImageView.translatesAutoresizingMaskIntoConstraints=NO;
productImageView.clipsToBounds=YES;
productImageView.contentMode=UIViewContentModeScaleAspectFill;
productImageView.layer.cornerRadius = 22.5;
productImageView.layer.masksToBounds = YES;
productImageView.layer.borderColor =[UIColor whiteColor].CGColor;
productImageView.layer.borderWidth = 1;
[self.contentView addSubview:productImageView];
/* Intialize and assign List icon Image */
listIconImage=[[UIImageView alloc]init];
listIconImage.translatesAutoresizingMaskIntoConstraints=NO;
listIconImage.image=[UIImage imageNamed:@"right-arrow.png"];
[self.contentView addSubview:listIconImage];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productImageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:7]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:productImageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeLeft
multiplier:1
constant:11.5]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:45]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:45]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productName
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.productImageView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:10]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productName
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:5]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productOption
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.productImageView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:10]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.productOption
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.productName
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:5]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.listIconImage
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-15.0f]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.listIconImage
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:20.0f]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.listIconImage
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:11.0f]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.listIconImage
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]]; `
上面是我用来创建这个Design的代码。现在我正在努力学习如何在tableviewcontroller中获得动态高度。当我进入heightForRowAtIndexPath时,它总是返回0。
谢谢&问候 Sam.P
答案 0 :(得分:0)
你应该设置tableView.rowHeight = UITableViewAutomaticDimension并提供tableView.estimatedRowHeight = 68.0f(估计高度,不必是准确的); 更多信息在以下链接: SO Link