【发布时间】:2016-08-05 09:28:59
【问题描述】:
实际上我想动态设置按钮并且按钮的标题出现动态我已经尝试过这个并获得了动态内容的宽度但我没有设置单元格的宽度和高度。
- (void)viewDidLoad {
[super viewDidLoad];
ContentArray = [[NSMutableArray alloc]init];
self.collectionview.delegate = self;
self.collectionview.dataSource =self;
self.collectionview.backgroundColor = [UIColor clearColor];
[ContentArray addObject:@"Planning"];
[ContentArray addObject:@"Resources"];
[ContentArray addObject:@"Human Resources"];
[ContentArray addObject:@"Agriculture"];
[ContentArray addObject:@"Resources management"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ContentArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell * Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell"forIndexPath:indexPath];
[Cell.button setTitle:[ContentArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
return Cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect rect = [[ContentArray objectAtIndex:indexPath.row ] boundingRectWithSize:CGSizeMake(HUGE_VALF, 50) options:NSStringDrawingUsesFontLeading attributes:nil context:nil] ;
return CGSizeMake(rect.size.width, 50);
}
输出是这样的
如果我让单元格动态宽度和高度,我认为这是完美的,所以请指导我完成这个。
【问题讨论】:
-
你想让单元格宽度足够大以显示单元格文本吗?
-
或单元格+标签高度显示全文?
-
在单元格中显示全文
-
您需要调整单元格和单元格内标签的大小
-
问题应该很清楚,
guide me through this听起来不太清楚。
标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout