iOS自动布局动态按钮放置

时间:2013-08-21 02:42:44

标签: ios uitableview constraints autolayout

我有一个带有两个堆叠标签的原型单元格,后面跟一个最多包含三个按钮的视图。对于任何给定的单元格(基于web xml文件),可以看到0,1,2或3个按钮。我希望布局如下(X代表按钮)

3个按钮

  |       |                   
  | X X X |         
  |       |

2个按钮

  |       |                   
  | X   X |         
  |       |

1个按钮

  |       |                   
  |   X   |         
  |       |

我的表格单元格代码根据单元格中的按钮总数设置每个按钮的框架。然而,框架确实有效,直到第二次装入单元格(即滚动使单元格超出范围然后返回到它)。上次发生这是由于布局限制。问题是,如何在故事板中设置约束以允许每个按钮位于许多位置?目前,每个按钮都固定在按钮视图的顶部,底部和前缘。我知道前沿约束导致问题,但不知道如何使用,因为故事板强制它。或者我的cellForRowAtIndexPath方法中有什么可以做的吗?以下是该方法的相关代码

    static NSString *CellIdentifier = @"localdiningTableCell";

        mhgiLocalDiningCell *diningcell = (mhgiLocalDiningCell*)[tableView
                                           dequeueReusableCellWithIdentifier:CellIdentifier];
        if (diningcell == nil) {
            diningcell = [[mhgiLocalDiningCell alloc]
                          initWithStyle:UITableViewCellStyleDefault
                          reuseIdentifier:CellIdentifier];
        }

        mhgiDiningXmlObject *object = [self.xmlParser.allItems objectAtIndex:[indexPath row]];

        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

        CGSize labelSize = [[object Name] sizeWithFont:diningcell.nameLabel.font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        CGRect frame = CGRectMake (10, 10, 280.0f, labelSize.height);
        diningcell.nameLabel.frame = frame;
        diningcell.nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
        diningcell.nameLabel.numberOfLines = 0;
        diningcell.nameLabel.text= [object Name];
        CGFloat nextYVal = diningcell.nameLabel.frame.origin.y + diningcell.nameLabel.frame.size.height + 10.0f;



        NSString *descrText = @"";
        if (![[object Type] isEqualToString:@""]){
            descrText = [descrText stringByAppendingString:[object Type]];
            descrText = [descrText stringByAppendingString:@"\n"];
        }
        descrText = [descrText stringByAppendingString:[NSString stringWithFormat:@"%@, %@, %@", [object Street], [object City], [object State]]];
        descrText = [descrText stringByAppendingString:@"\n"];
        descrText = [descrText stringByAppendingString:[NSString stringWithFormat:@"%@ miles from the hotel", [object Distance]]];




        labelSize = [descrText sizeWithFont:diningcell.typeLabel.font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        frame = CGRectMake (10, nextYVal, 280.0f, labelSize.height);
        diningcell.typeLabel.frame = frame;
        diningcell.typeLabel.lineBreakMode = NSLineBreakByWordWrapping;
        diningcell.typeLabel.numberOfLines = 0;
        diningcell.typeLabel.text = descrText;
        nextYVal += diningcell.typeLabel.frame.origin.y + diningcell.typeLabel.frame.size.height + 10.0f;


        diningcell.ussite= [object UrbanSpoon];
        diningcell.website= [object Website];
        diningcell.dirsite= [object Maps];

        NSMutableArray* buttons = [[NSMutableArray alloc] init];
        [buttons removeAllObjects];


        if ([[object Website] isEqualToString:@""]){
            [diningcell.websiteButton setHidden:TRUE];
        }
        else
        {
            [diningcell.websiteButton setHidden:FALSE];
            [buttons addObject:diningcell.websiteButton];
        }



        if ([[object UrbanSpoon] isEqualToString:@""]){
            [diningcell.usButton setHidden:TRUE];
        }
        else
        {
            [diningcell.usButton setHidden:FALSE];
            [buttons addObject:diningcell.usButton];
        }

        if ([[object Maps] isEqualToString:@""]){
            [diningcell.dirButton setHidden:TRUE];
        }
        else
        {
            [diningcell.dirButton setHidden:FALSE];
            [buttons addObject:diningcell.dirButton];
        }




        if (buttons.count == 1){
            [[buttons objectAtIndex:0] setCenter:CGPointMake(150.0f, 25.0f)];

        }
        else if (buttons.count == 2){
            [[buttons objectAtIndex:0] setCenter:CGPointMake(75.0f, 25.0f)];

            [[buttons objectAtIndex:1] setCenter:CGPointMake(225.0f, 25.0f)];
        }
        else if (buttons.count == 3){
            [[buttons objectAtIndex:0] setCenter:CGPointMake(75.0f, 25.0f)];

            [[buttons objectAtIndex:1] setCenter:CGPointMake(150.0f, 25.0f)];

            [[buttons objectAtIndex:2] setCenter:CGPointMake(225.0f, 25.0f)];
        }

        return diningcell;

更新 我创建了一个自定义视图来尝试并解决布局问题。该视图只包含四个按钮,并将它们放在layoutviews方法上。我在我的原型单元格中添加了自定义类的视图,布局似乎有效。然而,当我改变scrollveiw中的方向时(即向下然后向上,向上然后向下),一些单元格会不对齐。见下图。

Image

1 个答案:

答案 0 :(得分:0)

我会为三种细胞类型使用不同的原型,并在故事板中设置对齐。你最终只需要很少的代码。只需在cellForRowAtIndexPath中将相应类型的单元格出列并设置它 - 无需直接操作框架。