ios – 具有动态高度的UICollectionView单元格之间的空间不会相同

前端之家收集整理的这篇文章主要介绍了ios – 具有动态高度的UICollectionView单元格之间的空间不会相同前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有类似的问题 like this

我在运行时生成视图的高度.这是我的代码

  1. @interface CollectionViewController ()
  2. {
  3. NSMutableArray *arrMain;
  4. }
  5. @property(nonatomic,strong) NSMutableArray *arrMain;
  6. @end
  7.  
  8. @implementation CollectionViewController
  9. @synthesize arrMain,- (void)viewDidLoad
  10. {
  11. [super viewDidLoad];
  12.  
  13. [cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
  14.  
  15. CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
  16. self.cView.collectionViewLayout = fl;
  17.  
  18. NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
  19. SBJSON *parser = [[SBJSON alloc] init];
  20.  
  21. self.arrMain = [[NSMutableArray alloc] init];
  22. self.arrMain = [parser objectWithString:strJson error:nil];
  23. for (int i=0; i<[self.arrMain count]; i++) {
  24. NSDictionary *dic = [self.arrMain objectAtIndex:i];
  25. [self setTags:[[UIView alloc] init] selDictionary:dic]; // This function generates height and save it to the dictionary
  26. }
  27. [cView reloadData];
  28. }
  29.  
  30. - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
  31. {
  32. return [self.arrMain count];
  33. }
  34. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  35. {
  36.  
  37. NSDictionary *dic = [self.arrMain objectAtIndex:indexPath.row];
  38. return CGSizeMake(236,40+[[dic valueForKey:@"TAGS_HEIGHT"] intValue]);
  39. }
  40. - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
  41. {
  42.  
  43. CViewCell *cell =(CViewCell *) [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
  44. NSDictionary *dic = [self.arrMain objectAtIndex:indexPath.row];
  45. if ([cell viewWithTag:11]){
  46. [[cell viewWithTag:11] release];
  47. [[cell viewWithTag:11] removeFromSuperview];
  48. }
  49. UIView *viewTags = [[UIView alloc] init];
  50. [viewTags setTag:11];
  51. [viewTags setBackgroundColor:[UIColor lightGrayColor]];
  52. [self setTags:viewTags selDictionary:dic];
  53. [viewTags setFrame:CGRectMake(5,10,CONTENT_WIDTH,[[dic valueForKey:@"TAGS_HEIGHT"] floatValue])];
  54. [cell.contentView addSubview:viewTags];
  55.  
  56. return cell;
  57. }

我试过上面的链接解决方案,但它不适用于我.
这是我的输出的图像.

我需要的间距是相同的.
有人有解决这个问题的人吗?

这是UICOllectionView的错误吗?因为我在this article也发现了这个问题.

解决方法

我有一个这样的问题的一般解决方案:
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. [cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
  6.  
  7. CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
  8. fl.minimumInteritemSpacing = 10;
  9. fl.scrollDirection = UICollectionViewScrollDirectionVertical;
  10. self.cView.collectionViewLayout = fl;
  11.  
  12. NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
  13. SBJSON *parser = [[SBJSON alloc] init];
  14.  
  15. self.arrMain = [[NSMutableArray alloc] init];
  16. self.arrMain = [parser objectWithString:strJson error:nil];
  17. for (int i=0; i<[self.arrMain count]; i++) {
  18. NSDictionary *dic = [self.arrMain objectAtIndex:i];
  19. [self setTags:[[UIView alloc] init] selDictionary:dic]; // This function generates height and save it to the dictionary
  20. }
  21. [cView reloadData];

}

然后,更新CViewFlowLayout.m中的代码,它是UICollectionViewFlowLayout的一个子类.

这是代码

  1. #define numColumns 3
  2. @implementation CViewFlowLayout
  3. @synthesize numOfColumnsToDisplay;
  4. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  5. {
  6. NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
  7. for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
  8. {
  9. if (nil == attributes.representedElementKind)
  10. {
  11. NSIndexPath* indexPath = attributes.indexPath;
  12. attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
  13. }
  14. }
  15. return attributesToReturn;
  16. }
  17.  
  18. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
  19. {
  20. UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
  21.  
  22. if (indexPath.item < numColumns){
  23. CGRect f = currentItemAttributes.frame;
  24. f.origin.y = 0;
  25. currentItemAttributes.frame = f;
  26. return currentItemAttributes;
  27. }
  28. NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
  29. CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
  30. CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
  31. CGRect f = currentItemAttributes.frame;
  32. f.origin.y = YPointNew;
  33. currentItemAttributes.frame = f;
  34. return currentItemAttributes;
  35. }

解决方案仅适用于垂直滚动.如果要显示更多或更少的列,只需更改numColumns.

猜你在找的iOS相关文章