如何在UICollectionView的节头中动态添加标签和按钮?

时间:2016-06-17 05:07:44

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionreusableview

请帮助我如何水平添加标签和类似水平按钮,但每个按钮应该在每个标签的下方对齐,就像另一个部分一样。 这应该在UICollectionView的标题中动态发生,因为标签和按钮的数量是根据我的数据。

我想制作一种excel类型的布局,在标题中我想显示上述控件。

提前致谢!

我已经这样做了 -

DECLARE @P1 INT, @P2 DATETIME, @P3 DATETIME

SELECT [LINE_NO], [CUST_ORDER_ID], [PART_ID], [CUSTOMER_PART_ID], [MISC_REFERENCE], [PROMISE_DATE], [PROMISE_DEL_DATE] 
FROM [CUST_ORDER_LINE] 
WHERE [CUST_ORDER_ID] = @P1 
AND (@P2 IS NULL OR [PROMISE_DATE] = @P2)
AND (@P3 IS NULL OR [PROMISE_DEL_DATE] = @P3)

OfficeList是我的数组,其中包含我希望在索引值的每个标签中显示的列表。

3 个答案:

答案 0 :(得分:2)

这完全正常: -

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

    CGFloat x=0,y=0;

    for (int i = 0;i<[_officelist count];i++)
    {
        id val=[officeSize objectAtIndex:i];
        CGFloat val1=[val floatValue];
            UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10,val1-1,35)];

            newLabel.text=[NSString stringWithFormat:@"%@",_officelist[i]];

            newLabel.textAlignment = NSTextAlignmentCenter;

            newLabel.backgroundColor = [UIColor greenColor];
            [self.roadmapCollectionView addSubview:newLabel];

        x=x+val1+1;
    }
    for (int i=0; i<_departmentlist.count; i++) {

        dept=[_departmentlist objectAtIndex:i];
        id val=[officeSize objectAtIndex:i];
        CGFloat val1=[val floatValue];

        float val2=val1/[dept count];
            //NSLog(@"DEPT SIZE - %f",val2);

            for (int j = 0; j < [dept count]; j++)
            {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(y, 50,val2-1, 25);
                [button setBackgroundColor:[UIColor yellowColor]];
                [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                [button setTitle:[NSString stringWithFormat:@"%@",dept[j]] forState:UIControlStateNormal];

                [self.roadmapCollectionView addSubview:button];

                [deptSize addObject:[NSNumber numberWithFloat:y]];

                y=y+val2+1;

            }
}

    return reusableView;
}

答案 1 :(得分:1)

UICollectionView提供回调 -

  • 每当它显示supplementaryView

    - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
    
  • 只要supplementaryView滚出屏幕 -

    - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
    

您需要在此处注明elementKind参数。它有两个值UICollectionElementKindSectionHeader&amp; UICollectionElementKindSectionFooter

我认为您需要使用UICollectionElementKindSectionHeader进行自定义。

希望它有所帮助。

更新

如果UICollectionView无法提供如上所述的动态自定义方法,建议的方法是在故事板中设计UICollectionElementKindSectionHeader

您需要做的就是在视图层次结构中的UICollectionReusableView内拖动collectionView。您可能还需要考虑为此设置自定义子类,并将IBOutlet连接添加到不同的UI组件。

答案 2 :(得分:0)

试试这段代码。

更新:尝试以编程方式为uilabel设置约束。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {

        DepartmentCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        UIFont * customFont = [UIFont fontWithName:ProximaNovaSemibold size:12];
        CGSize labelSize = [text sizeWithFont:customFont constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
        label.tag = indexPath.row;
        label.text=[NSString stringWithFormat:@"%@",_officelist[indexPath.row]];
        label.font = customFont;
        label.numberOfLines = 1;
        label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; 
        label.adjustsFontSizeToFitWidth = YES;
        label.adjustsLetterSpacingToFitWidth = YES;
        label.minimumScaleFactor = 10.0f/12.0f;
        fromLabel.clipsToBounds = YES;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        [self.roadmapCollectionView addSubview:label];
        reusableview = headerView;
    }
    return reusableview;
}

我建议你在故事板中创建一个uiview,并在每次测量显示它必须充气时给它充气。