How to dynamically add labels and buttons to a UICollectionView section header?

Please help me, how can I add labels horizontally and similarly with the buttons horizontally, but each button should align with each label, as in another section. This should happen dynamically in the UICollectionView header, since the number of labels and buttons matches my data.

I want to make excel a layout view, and in the header I want to show the controls described above.

Thanks in advance!

I did it -

- (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 is my array containing a list that I want to display on each label with an index.

+4
source share
3 answers

This works fine: -

 - (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; } 
+2
source

UICollectionView provides callbacks -

  • Whenever supplementaryView is displayed

     - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath 
  • Whenever a supplementaryView scrolls from the screen -

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

You need to look for the elementKind parameter elementKind . It has two values: UICollectionElementKindSectionHeader and UICollectionElementKindSectionFooter .

I believe that you need to make your settings using the UICollectionElementKindSectionHeader .

Hope this helps.

UPDATE:

If the UICollectionView does not provide a way to configure this on the fly, as described above, the recommended approach would be to develop a UICollectionElementKindSectionHeader in the storyboard itself.

All you have to do is drag the UICollectionReusableView inside the collectionView into the view hierarchy. You may also need to set up a custom subclass for this and add IBOutlet connections to the various components of the user interface.

+1
source

Try this code.

UPDATE: Try setting software restrictions for 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; } 

I would recommend you create a uiview in the storyboard and inflate it every time the measurements show that you need to inflate it.

0
source

All Articles