Greg Miller
Greg Miller

Reputation: 479

Custom UITableViewCell view disappears autoligned to right

Been banging at this for a couple of days now off and on. The issue is that when I use autolayout constraints and align a view on the right edge of a UITableViewCell, the view disappears. Below is code that reproduces the issue, but I've tried lots of other things like various "needsLayout", "needsUpdateContraints", etc. in various places along with their respective "updateLayoutIfNeeded" calls all over the place. I have also tried overriding "updateConstraints" and adding the constraints there. But I get the exact same results no matter what I do.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* SimpleIdentifier=@"Simple";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:SimpleIdentifier];
tableView.translatesAutoresizingMaskIntoConstraints=NO;

    UILabel* label1;
    UILabel* label2;

    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleIdentifier];

        label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
        label1.tag=100;
        label1.translatesAutoresizingMaskIntoConstraints=NO;
        [cell.contentView addSubview:label1];

        label2=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 100, 50)];
        label2.tag=101;
        label2.translatesAutoresizingMaskIntoConstraints=NO;
        [cell.contentView addSubview:label2];

        NSDictionary* viewsDictionary=@{@"label1":label1,@"label2":label2};
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label1]" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label2]|" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
        //[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label1]-[label2]" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
    } else {
        label1=(UILabel*)[cell.contentView viewWithTag:100];
        label1=(UILabel*)[cell.contentView viewWithTag:101];
    }

    label1.text=@"Label 1";
    label2.text=@"Label 2";

    return cell;
}

Upvotes: 0

Views: 185

Answers (1)

Leo
Leo

Reputation: 24714

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* SimpleIdentifier=@"Simple";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:SimpleIdentifier];

UILabel* label1;
UILabel* label2;

if(cell==nil){
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleIdentifier];

    label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    label1.tag=100;
    label1.translatesAutoresizingMaskIntoConstraints=NO;
    [cell.contentView addSubview:label1];

    label2=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 100, 50)];
    label2.tag=101;
    label2.translatesAutoresizingMaskIntoConstraints=NO;
    [cell.contentView addSubview:label2];

    NSDictionary* viewsDictionary=@{@"label1":label1,@"label2":label2};
    [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label1]" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
    [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label2]|" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
    [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label1]|" options:0 metrics:nil views:viewsDictionary]];
      [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label2]|" options:0 metrics:nil views:viewsDictionary]];

} else {
    label1=(UILabel*)[cell.contentView viewWithTag:100];
    label1=(UILabel*)[cell.contentView viewWithTag:101];
}

label1.text=@"Label 1";
label2.text=@"Label 2";

return cell;

}

And screenshot enter image description here

Upvotes: 1

Related Questions