【发布时间】:2014-10-10 08:07:34
【问题描述】:
如 WWDC 2014 所示,我有 UICollection 视图使用自定尺寸单元格。
我想把它放在 UITableViewCell 中并显示一组标签(我想要实现的目标的良好参考是在 Foursquare 应用程序中:)
我的问题是,使用自行调整大小的单元格时,内容大小返回不正确。 我准备了一些快速演示来展示问题:
- (void)viewDidLoad
{
[super viewDidLoad];
self.elements = [NSMutableArray array];
for (int i = 0; i < 23; i++)
{
[self.elements addObject:[self randomString]];
}
UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
layout.estimatedItemSize = CGSizeMake(50, 30);
self.collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CustomCell"];
[self.collectionView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:self.collectionView];
}
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.collectionView.frame = CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.contentSize.height);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.elements count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
cell.customLabel.text = [self.elements objectAtIndex:[indexPath row]];
return cell;
}
当我用旧方法删除estimatedItemSize 并改用itemSize 或委托方法来计算大小时 - 它工作正常,所以我认为它使用estimatedItem 大小来获取内容大小。
有什么方法可以让它与自调整大小的单元格一起工作,并根据它的内容自动调整 UICollectionView 的大小?
【问题讨论】:
-
err... 我知道这没有帮助,但我建议现在不要在集合视图中使用自调整单元格。似乎 Apple 在实现这些 devforums.apple.com/… 方面做得不太好。
-
那么按照我的意愿实现它的方法是什么? :) 即使使用手动计算,我也无法预测 UICollectionView 需要多少空间来显示其内容 - 我需要预测 UITableViewCell 高度 :)
标签: ios objective-c uicollectionview uicollectionviewlayout