现在已经打了好几天了。问题是,当我使用自动布局约束并在UITableViewCell的右边缘对齐视图时,视图会消失。下面是重现问题的代码,但我已经尝试了很多其他的东西,比如各种各样的“needsLayout”,“needsUpdateContraints”等,以及它们各自的“updateLayoutIfNeeded”调用。我也尝试重写“updateConstraints”并在那里添加约束。但无论我做什么,我都会得到完全相同的结果。
-(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;
}
答案 0 :(得分:1)
-(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;
}
截图