这应该在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的节头中动态添加标签和按钮?全部内容,希望文章能够帮你解决ios – 如何在UICollectionView的节头中动态添加标签和按钮?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)