当我使用auto_layouts在uitableviewcell上添加标签时显示异常

时间:2015-09-03 13:14:32

标签: ios objective-c uitableview autolayout

您好我想要使用auto在tableview单元格上添加UILabel _layouts以编程方式约束项目,但标签不在单元格上添加

添加如下图像

enter image description here

我在这里做错了什么。

我的代码如下:

- (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;
}

0 个答案:

没有答案