简体   繁体   English

ios7自动布局tableViewCell

[英]ios7 AutoLayout tableViewCell

I currently have a custom UITableViewCell with a number of labels in it. 我目前有一个带有许多标签的自定义UITableViewCell Not all of those labels will necessarily have a value but they could do - and they may be multiline (At least 2 labels will always be visible). 并非所有这些标签都必须具有值,但是它们可以做到-它们可能是多行的(至少2个标签将始终可见)。 If there isn't any text then the label should collapse. 如果没有任何文字,则标签应折叠。 What I'm trying to achieve is that each label only takes up the height it needs and they all butt up to each other. 我要实现的目标是,每个标签仅占据其所需的高度,并且它们彼此对接。 I've got all of this working, except I finally want a bottom margin of 11 between the last label and the cells content view with the cell height adjusted accordingly. 我已经完成了所有这些工作,除了我最终希望最后一个标签和单元格内容视图之间的底边距为11,并相应地调整了单元格的高度。 This is the only bit where I can't seem to get my constraints working correctly. 这是我似乎无法使约束正常工作的唯一方面。

I correctly return the right height by creating a dummy cell, and in my cellForRowAtIndexPath method I try to set the cell and the cell's contentview frame accordingly. 我通过创建一个虚拟单元格正确地返回了正确的高度,并且在我的cellForRowAtIndexPath方法中,我尝试相应地设置该单元格和该单元格的contentview框架。 But the cell height that is rendered on the screen is always the same as it was defined in the xib. 但是,在屏幕上呈现的像元高度始终与xib中定义的高度相同。

I know there's a lack of code here, but all my constraints have been define in IB and each label has a trailing and leading space for with and a top space of 0 to the label above it. 我知道这里缺少代码,但是我所有的约束都已在IB中定义,每个标签的尾部和前导空格为,上方的标签顶部为0。 The top label has a top space to create a top margin to the content view, but as soon as I do this on the bottom label it stretches the height of the labels to accommodate all the constraints . 顶部标签具有顶部空间,可为内容视图创建顶部边缘,但是一旦我在底部标签上执行此操作,它就会拉伸标签的高度以适应所有约束

I've seen various tutorials and questions on here that relate to 2 labels where 1 might be a variable size, but I haven't been able to adapt any solutions for what I'm trying to achieve. 我在这里看到了许多与2个标签相关的教程和问题,其中1个标签可能是可变大小,但是我无法针对我要实现的目的调整任何解决方案。

Okay, 好的,

  1. Get as many labels you want (suppose you want 2 labels inside the cell,) Create 2 labels with height 1 in storyboard, or if you are programmatically adding them, then you can do. 获取任意数量的标签(假设在单元格中需要2个标签),在情节提要中创建2个高度为1的标签,或者以编程方式添加它们,则可以这样做。

     UILabel *myLabel1 = [[UILabel alloc]initWithFrame:[CGRectMake:(x,y,width,1)]; // assuming you know their x,y and width respectively as you need them. 
  2. Now add constraints to the labels (Be sure you make them ambiguous or else orientation would mess it up, ): 现在,向标签添加约束(请确保使它们不明确,否则方向会弄乱它,):

    3 a. 3个 Height -(should be equalto OR greater than => 1). 高度-(应等于或大于=> 1)。 b. Width - [Set it a width],Make sure it has a priority more than others but less than height c. 宽度-[设置宽度],请确保其优先级高于其他,但小于高度c。 Pin to trailing and leading space, as well as superview top and bottom. 固定在尾随和前导空间以及超级视图的顶部和底部。 d. d。 If there are two Labels make sure you click both of them and add a Vertical or horizontal space according to your needs. 如果有两个标签,请确保单击两个标签,然后根据需要添加垂直或水平空间。

    I think that would make it ambiguous. 我认为这将使其变得模棱两可。 (I am telling these as you wrote its about autolayout, so despite of orientation this would never get any layout problems). (我是在您撰写有关自动布局的文章时告诉他们的,所以尽管有方向性,也永远不会出现任何布局问题)。

    Add a snippet that would calculate the height of the Label when you know the string. 添加一个片段,该片段将在您知道字符串时计算Label的高度。

      CGSize constrainedSizeOfMessege = CGSizeMake(widthOfLabel, 9999);//150,180 anything you like NSDictionary *attributesDictionaryMessege = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"System Italics" size:16.0], NSFontAttributeName, nil]; NSMutableString *myString = [[NSMutableString alloc]initWithString:[[friendsInstanceArray objectAtIndex:indexPath.row] myString] attributes: attributesDictionaryMessege]; CGRect requiredHeightMessege = [myString boundingRectWithSize:constrainedSizeOfMessege options:NSStringDrawingUsesLineFragmentOrigin context:nil]; 

Add the above code in 在上面添加代码

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Now, calculate the height of the Cell, the height of all the labels if they are 1 height then Return the height of the Cell in storyboard + height of the Expected Label Size. 现在,计算单元格的高度,如果所有标签的高度均为1,则计算它们的高度,然后在情节提要中返回单元格的高度+预期标签大小的高度。

So The method would look like. 因此该方法看起来像。

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {
  CGSize constrainedSizeOfMessege = CGSizeMake(widthOfLabel, 9999);//150,180 anything you like
      NSDictionary *attributesDictionaryMessege = [NSDictionary dictionaryWithObjectsAndKeys:
        [UIFont fontWithName:@"System Italics" size:16.0], NSFontAttributeName,
    nil]; 
       NSMutableString *myString = [[NSMutableString alloc]initWithString:[[friendsInstanceArray objectAtIndex:indexPath.row] myString] attributes: attributesDictionaryMessege];
     CGRect requiredHeightMessege = [myString boundingRectWithSize:constrainedSizeOfMessege options:NSStringDrawingUsesLineFragmentOrigin context:nil];
  int heightOfMessege = requiredHeightMessege.size.height;
       //if cell height is 50 then return 50+heightOfMessege
 return 50+heightOfMessege;
}

And YES POSITIVELY set, mylabel.numberOfLines = 0; 并且肯定设置为mylabel.numberOfLines = 0;

Hope this helps you out 希望这可以帮助你

如果您想将标签粘贴在包括空白在内的位置上,请在标签的标题文本中留出一个空格,这不会使他们的文本为空,然后标签会粘贴在位置上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM