【问题标题】:iPhone Custom cell with consecutive UILabels具有连续 UILabel 的 iPhone 自定义单元格
【发布时间】:2015-03-25 09:40:18
【问题描述】:

我正在尝试使用 4 个连续的 UILabel 实现自定义单元格(见下图)。在两个连续标签之间将包含 1 px 的间隙,并且 4 个标签必须水平填充单元格。我找不到任何方法在情节提要中实现这个(独特的设计)。所以我以编程方式做到了。但是通过以编程方式进行操作,如果我定位我的设备,我的表格视图单元格如下图(02)所示,其中 4 个标签不会水平填充整个单元格。我尝试使用 autoResizingMask,但这不起作用。如何以编程方式为此自定义单元格实现自动布局功能?或者有没有更好的想法在情节提要中使用自动布局功能?

在我的自定义单元实现文件中,我尝试了如下代码 -

- (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.

02.

【问题讨论】:

  • 我创建了一个示例项目,解决了您的问题。请从here下载。我正在使用自动布局。让我知道,如果您遇到任何问题。希望,它会有所帮助。
  • 谢谢,它对我有用。但我不希望单元格中每个标签的顶部和底部有任何额外的空白。我的意思是每个标签都应该填满单元格的总高度,就像每个标签的背景颜色触摸/重叠单元格分隔线一样。我试过 [self.myLabel sizeToFit];但这不起作用。我怎样才能做到这一点 ?请让我知道我该怎么办?
  • 在单元格的 xib 中将所有 13 的“垂直空间约束常数”更改为 -8(或零,取决于您的需要)。我已经更新了我的代码。再次参考相同的链接。

标签: ios objective-c uitableview autolayout custom-cell


【解决方案1】:

使用故事板。将所有标签设置为具有相同的宽度并连接它们 -label1-label2-label3-label4-。还将它们连接到顶部和底部。

【讨论】:

    【解决方案2】:

    您可以更轻松地在情节提要中做到这一点。你只需要给这四个视图'等宽''等高'约束以及适当的尾随和前导约束。要提供“等宽”“等高”,请选择情节提要中的所有视图,然后转到编辑器 -> 引脚 -> 等宽/等高。 希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      你也可以使用 UITableView。将您的自定义视图设置为顶部,然后在视图下方添加一个表格视图,创建自定义 UITableViewCell 并使用您的对象初始化每个单元格。您可以根据需要自定义单元格。这种方式比我认为的自动布局更安全。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-04
        • 1970-01-01
        • 2023-04-09
        • 2011-04-22
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多