如何在UicollectionView的截面标题中动态添加标签和按钮



请帮助我如何水平和类似的按钮添加标签,但是每个按钮都应像另一个部分一样在每个标签的向下对齐。由于标签和按钮的数量是根据我的数据。

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

预先感谢!

我已经做到了 -

- (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];
        for (int i=0; i<_officelist.count; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.tag = i+1;
            label.text=[NSString stringWithFormat:@"%@",_officelist[i]];
            [self.roadmapCollectionView addSubview:label];
        }
        reusableview = headerView;
    }
    return reusableview;
}

官员是我的数组,其中包含我想在索引值的每个标签中显示的列表。

这很好地工作了: -

- (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;
}

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组件。

尝试此代码。

更新:尝试以编程方式为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,并每次测量表明必须夸大它。

相关内容

  • 没有找到相关文章

最新更新