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

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionreusableview

请帮助我如何水平添加标签和水平添加类似的按钮,但每个按钮应该像另一个部分一样在每个标签的下方对齐。 这应该在 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;

}

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

最佳答案

这工作得很好:-

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

关于ios - 如何在 UICollectionView 的节标题中动态添加标签和按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873537/

相关文章:

ios - 接收有关添加的 Firebase 数据库子级的推送通知

ios - 在 Spritekit 的 SKShapeNode 中将特定点居中

iphone - 即使在 iphone 中使用异步调用后下载图像也很慢

ios - 如何使用 PHPhotoLibrary 将视频/图像写入保存的相册?

ios - 如何使用 CollectionView/TableView Controller 实现滑动 View ?

objective-c - 多个 View Controller 中的 CLLocation

c++ - OpenCV 透视校正和裁剪

ios - NSPredicate完全匹配

ios - 包装在 UIViewController 中的 UICollectionView

iphone - 有没有办法找到 UICollectionView 部分的索引?