[英]UICollectionView with dynamic height not getting same space between cell
我有類似的問題, 這樣的
我在運行時生成視圖高度。這是我的代碼
@interface CollectionViewController ()
{
NSMutableArray *arrMain;
}
@property(nonatomic,strong) NSMutableArray *arrMain;
@end
@implementation CollectionViewController
@synthesize arrMain,
- (void)viewDidLoad
{
[super viewDidLoad];
[cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
self.cView.collectionViewLayout = fl;
NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
SBJSON *parser = [[SBJSON alloc] init];
self.arrMain = [[NSMutableArray alloc] init];
self.arrMain = [parser objectWithString:strJson error:nil];
for (int i=0; i<[self.arrMain count]; i++) {
NSDictionary *dic = [self.arrMain objectAtIndex:i];
[self setTags:[[UIView alloc] init] selDictionary:dic]; // This function generates height and save it to the dictionary
}
[cView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return [self.arrMain count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [self.arrMain objectAtIndex:indexPath.row];
return CGSizeMake(236,40+[[dic valueForKey:@"TAGS_HEIGHT"] intValue]);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
CViewCell *cell =(CViewCell *) [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
NSDictionary *dic = [self.arrMain objectAtIndex:indexPath.row];
if ([cell viewWithTag:11]){
[[cell viewWithTag:11] release];
[[cell viewWithTag:11] removeFromSuperview];
}
UIView *viewTags = [[UIView alloc] init];
[viewTags setTag:11];
[viewTags setBackgroundColor:[UIColor lightGrayColor]];
[self setTags:viewTags selDictionary:dic];
[viewTags setFrame:CGRectMake(5, 10, CONTENT_WIDTH, [[dic valueForKey:@"TAGS_HEIGHT"] floatValue])];
[cell.contentView addSubview:viewTags];
return cell;
}
我曾嘗試過上面的鏈接解決方案,但它對我不起作用。 這是我輸出的圖像。
我需要間距相同。 有人有解決這個問題的方法嗎?
這是UICOllectionView的錯誤嗎? 因為我在本文中也發現了這個問題。
我有這種問題的一般解決方案:
- (void)viewDidLoad
{
[super viewDidLoad];
[cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
fl.minimumInteritemSpacing = 10;
fl.scrollDirection = UICollectionViewScrollDirectionVertical;
self.cView.collectionViewLayout = fl;
NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
SBJSON *parser = [[SBJSON alloc] init];
self.arrMain = [[NSMutableArray alloc] init];
self.arrMain = [parser objectWithString:strJson error:nil];
for (int i=0; i<[self.arrMain count]; i++) {
NSDictionary *dic = [self.arrMain objectAtIndex:i];
[self setTags:[[UIView alloc] init] selDictionary:dic]; // This function generates height and save it to the dictionary
}
[cView reloadData];
}
然后,更新CViewFlowLayout.m中的代碼,它是UICollectionViewFlowLayout
的子類。
這是代碼:
#define numColumns 3
@implementation CViewFlowLayout
@synthesize numOfColumnsToDisplay;
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
{
if (nil == attributes.representedElementKind)
{
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
if (indexPath.item < numColumns){
CGRect f = currentItemAttributes.frame;
f.origin.y = 0;
currentItemAttributes.frame = f;
return currentItemAttributes;
}
NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
CGRect f = currentItemAttributes.frame;
f.origin.y = YPointNew;
currentItemAttributes.frame = f;
return currentItemAttributes;
}
此解決方案僅適用於垂直滾動。 如果要顯示更多或更少的列,只需更改numColumns
。
嘗試這個:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(250, 150);
}
您可以使用屬性設置大小:
flowLayout.headerReferenceSize = CGSizeMake(0, 100);//constant height for all the items header
如果動態:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if (section == albumSection) {
return CGSizeMake(0, 100);
}
return CGSizeZero;
}
在Storybord或xib設置值中看到這個!
從UICollectionViewFlowLayout
繼承的類必須包含以下方法:
- (CGSize)collectionViewContentSize
{
return CGSizeMake(200, 200) // as per your cell size
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
{
if (nil == attributes.representedElementKind)
{
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* currentItemAttributes =
[super layoutAttributesForItemAtIndexPath:indexPath];
if (!currentItemAttributes)
{
currentItemAttributes = [UICollectionViewLayoutAttributes
layoutAttributesForCellWithIndexPath:indexPath];
}
return currentItemAttributes;
}
並在您的CollectionViewController中添加
#pragma mark - Collection View Datasource
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Adjust cell size for orientation
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
{
return CGSizeMake(150, 230);
}
return CGSizeMake(150, 230);
}
使用以下方法
//迅速
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return CGSizeMake(250, 150);
}
//目標C.
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(250, 150);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.