您好我想要使用auto在tableview单元格上添加UILabel _layouts以编程方式约束项目,但标签不在单元格上添加
添加如下图像
我在这里做错了什么。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if(indexPath.section == 0)
{
formulaLabel = [[UILabel alloc]init];
formulaLabel.font = [UIFont fontWithName:@"Bitter-Bold" size:16.0f];
formulaLabel.translatesAutoresizingMaskIntoConstraints = NO;
formulaLabel.text = @"Ramakrishna";
formulaLabel.textColor = [UIColor orangeColor];
[cell.contentView addSubview:formulaLabel];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:formulaLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:40.f];
[cell.contentView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:formulaLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
[cell.contentView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:formulaLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f];
[cell.contentView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:formulaLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[cell.contentView addConstraint:constraint];
}
return cell;
}