我正在尝试使用4个连续UILabels实现自定义单元格(请参见下面的图像)。两个连续的标签之间将包含一个1 px的间隙,并且这四个标签必须水平填充该单元格。我找不到在故事板中实现这个(独特设计)的方法。所以我以编程方式做了。但通过编程方式,如果我定位我的设备,我的表格视图单元格看起来像下面的图像(02)其中4个标签不会水平填充整个单元格。我尝试过使用autoResizingMask,但这不起作用。我如何以编程方式实现此自定义单元格的自动布局功能?或者是否有更好的主意,在具有自动布局功能的情节提要中进行?具有连续UILabels的iPhone自定义单元格
在我的自定义单元格实现文件我试图像下面的代码 -
- (void)awakeFromNib {
// Initialization code
CGRect screenBounds = [[UIScreen mainScreen] bounds];
float screenWidth = (screenBounds.size.width - 4.0f)/4;
CGRect label1Container = CGRectMake(0, 2, screenWidth, 50);
UILabel *label1 = [[UILabel alloc] initWithFrame:label1Container];
label1.backgroundColor = [UIColor colorWithRed: 107.0f/255.0f green:199.0f/255.0f blue:190.0f/255.0f alpha:1.0];
label1.textAlignment = NSTextAlignmentCenter;
label1.numberOfLines = 0;
label1.text = @"Lotery\nSerial";
label1.font = [UIFont boldSystemFontOfSize:17];
[self.contentView addSubview:label1];
CGRect label2Container = CGRectMake(screenWidth + 1, 2, screenWidth, 50);
UILabel *label2 = [[UILabel alloc] initWithFrame:label2Container];
label2.backgroundColor = [UIColor colorWithRed: 107.0f/255.0f green:199.0f/255.0f blue:190.0f/255.0f alpha:1.0];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
label2.text = @"Bank\nCode";
label2.font = [UIFont boldSystemFontOfSize:17];
[self.contentView addSubview:label2];
CGRect label3Container = CGRectMake(screenWidth * 2 + 2, 2, screenWidth, 50);
UILabel *label3 = [[UILabel alloc] initWithFrame:label3Container];
label3.backgroundColor = [UIColor colorWithRed: 107.0f/255.0f green:199.0f/255.0f blue:190.0f/255.0f alpha:1.0];
label3.textAlignment = NSTextAlignmentCenter;
label3.numberOfLines = 0;
label3.text = @"Branch\nCode";
label3.font = [UIFont boldSystemFontOfSize:17];
[self.contentView addSubview:label3];
CGRect label4Container = CGRectMake(screenWidth*3+3, 2, screenWidth+1, 50);
UILabel *label4 = [[UILabel alloc] initWithFrame:label4Container];
label4.backgroundColor = [UIColor colorWithRed: 107.0f/255.0f green:199.0f/255.0f blue:190.0f/255.0f alpha:1.0];
label4.textAlignment = NSTextAlignmentCenter;
label4.numberOfLines = 0;
label4.text = @"Bank\nSerial";
label4.font = [UIFont boldSystemFontOfSize:17];
[self.contentView addSubview:label4];
//[self.label1 setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight)];
//[self.label2 setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
//[self.label3 setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
//[self.label2 setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight)];
}
01.
我已经创建了一个示例项目,解决了您的问题。请从[这里]下载(https://www.dropbox.com/sh/c7eotp89qihlkgb/AAAfMp6XT9c5LlOl8xJDiO9ea?dl=0)。我正在使用自动布局。如果您遇到任何问题,请告诉我。希望能帮助到你。 – itsji10dra 2015-03-25 10:54:11
谢谢,它为我工作。但是我不想在单元格中每个标签的顶部和底部添加任何额外的空白空间。我的意思是每个标签都应该填满单元总高度,就像每个标签的背景颜色接触/重叠单元格分隔线一样。我试着用[self.myLabel sizeToFit];但那不起作用。我怎样才能做到这一点 ?请让我知道我该怎么办? – Nuibb 2015-03-27 05:46:32
在你单元格的xib中将所有'垂直空间约束的常量'更改为13到-8(或零,取决于您的需要)。我更新了我的代码。再次参考相同的链接。 – itsji10dra 2015-03-27 06:22:38