• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 自定义 UICollectionViewLayout 不同的单元格大小

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:33:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想像这样自定义 UICollectionViewLayouthttp://i.stack.imgur.com/aWUIa.jpg

当我滚动第二页图像大小不适合单元格大小时; http://i.stack.imgur.com/GOUmF.jpg

然后再次返回第一页;
http://i.stack.imgur.com/lQQ3h.png

图像大小和单元格大小不匹配。 这是我的自定义布局代码;

//
//  SeyahatLayout.m
//  SeyahatTrend
//
//  Created by Mehmet Can Yavuz on 03.06.2013.
//  Copyright (c) 2013 Mehmet Can Yavuz. All rights reserved.
//

#import "SeyahatLayout.h"
#import "SeyahatLayoutAttributes.h"

#define kNumberOfItemsPerPage 5

static NSString * const BHPhotoAlbumLayoutPhotoCellKind = @"hotoCell";

@interface SeyahatLayout ()

@property (nonatomic, strong) NSDictionary *layoutInfo;

@end

@implementation SeyahatLayout

- (CGSize)collectionViewContentSize
{
    // Ask the data source how many items there are (assume a single section)
    id<UICollectionViewDataSource> dataSource = self.collectionView.dataSource;
    int numberOfItems = [dataSource collectionView:self.collectionView 
                            numberOfItemsInSection:0];

    // Determine how many pages are needed
    int numberOfPages = ceil((float)numberOfItems/(float)kNumberOfItemsPerPage);

    // Set the size
    float pageWidth = self.collectionView.frame.size.width;
    float pageHeight = self.collectionView.frame.size.height;
    CGSize contentSize = CGSizeMake(numberOfPages * pageWidth, pageHeight);

    return contentSize;
}

#pragma mark - Layout

- (void)prepareLayout
{
    NSMutableDictionary *newLayoutInfo = [NSMutableDictionary dictionary];
    NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];

    NSInteger sectionCount = [self.collectionView numberOfSections];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    for (NSInteger section = 0; section < sectionCount; section++) {
         NSInteger itemCount = 
           [self.collectionView numberOfItemsInSection:section];

        for (NSInteger item = 0; item < itemCount; item++) {
            indexPath = [NSIndexPath indexPathForItem:item inSection:section];

            SeyahatLayoutAttributes* attr = 
              [self layoutAttributesForItemAtIndex:item];
         // UICollectionViewLayoutAttributes *itemAttributes =
         //  [UICollectionViewLayoutAttributes 
         //     layoutAttributesForCellWithIndexPath:indexPath];
         // itemAttributes.frame = [self frameForAlbumPhotoAtIndexPath:indexPath];

            [cellLayoutInfo setObject:attr forKey:indexPath];
        }
    }

    newLayoutInfo[BHPhotoAlbumLayoutPhotoCellKind] = cellLayoutInfo;
    self.layoutInfo = newLayoutInfo;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:
       (NSIndexPath *)indexPath {

    return self.layoutInfo[BHPhotoAlbumLayoutPhotoCellKind][indexPath];
}

-(NSArray*)layoutAttributesForElementsInRectCGRect)rect
{
//  NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
//  NSMutableArray* attributes = [NSMutableArray array];
//  for (NSInteger i=0 ; i < itemCount; i++) {
//       NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i 
                                                      inSection:0];
//       [attributes addObject:
           [self layoutAttributesForItemAtIndexPath:indexPath]];
//  }
//    return attributes;
    NSLog(@"RECTX: %f",rect.origin.x );
    NSLog(@"RECTY: %f",rect.origin.y );
    NSLog(@"RECTWIDTH: %f",rect.size.width );
    NSLog(@"RECTHEIGHT: %f",rect.size.height );

    id<UICollectionViewDataSource> dataSource = self.collectionView.dataSource;
    int numberOfItems = [dataSource collectionView:self.collectionView 
                            numberOfItemsInSection:0];

    NSMutableArray* attributes = [NSMutableArray array];
    int page = rect.origin.x / 1024.0;
    if(numberOfItems>0){
    for(NSInteger i = 0; i<kNumberOfItemsPerPage; i++){
        int x = page*kNumberOfItemsPerPage + i;
        NSLog(@"%d",x);
        NSIndexPath* indexPath = [NSIndexPath indexPathForItem:x inSection:0];
        [attributes addObject:self.layoutInfo[BHPhotoAlbumLayoutPhotoCellKind]
           [indexPath]];
    }

    return attributes;
    }
    else
        return nil;

}

- (SeyahatLayoutAttributes *)layoutAttributesForItemAtIndexint)index
{
    NSIndexPath *path = [NSIndexPath indexPathForItem:index inSection:0];
    SeyahatLayoutAttributes *attributes = 
      (SeyahatLayoutAttributes *)[UICollectionViewLayoutAttributes  
                                    layoutAttributesForCellWithIndexPath:path]; 

    // Figure out what page this item is on
    int pageNumber = floor((float)index / (float) kNumberOfItemsPerPage);

    // Set the horizontal offset for the start of the page
    float pageWidth = self.collectionView.frame.size.width;
    float horizontalOffset = pageNumber * pageWidth;

    // Now, determine which position this cell occupies on the page.
    int indexOnPage = index % kNumberOfItemsPerPage;

    int column = 0;
    switch (indexOnPage) {
        case 0:
        case 1:
            column = 0;
            break;
        case 2:
        case 3:
        case 4:
            column = 3;
            break;
        default:
            column = 0;
            break;
    }

    int row = 0;
    switch (indexOnPage) {
        case 0:
        case 2:
            row = 0;
            break;
        case 3:
            row = 1;
            break;
        case 1:
        case 4:
            row = 2;
            break;
        default:
            row = 0;
            break;
    }

    horizontalOffset = horizontalOffset + ((175.0 + 30.0) * column) + 30.0;
    float verticalOffset = (194.0 + 30.0) * row + 30.0;

    // finally, determine the size of the cell.
    float width = 0.0;
    float height = 0.0;

    switch (indexOnPage) {
        case 0:
            width = 585.0;
            height = 418.0;
            break;
        case 1:
            width = 585.0;
            height = 194.0;
            break;
        default:
            width = 350.0;
            height = 194.0;
            break;
    }

    CGRect frame = CGRectMake(horizontalOffset, verticalOffset, width, height);
    [attributes setFrame:frame];

    return attributes;
}

+ (Class)layoutAttributesClass
{
    return [SeyahatLayoutAttributes class];
}

@end

这是我自定义的 uicollectionviewcell 代码;

//
//  SeyahatCell.m
//  SeyahatTrend
//
//  Created by Mehmet Can Yavuz on 03.06.2013.
//  Copyright (c) 2013 Mehmet Can Yavuz. All rights reserved.
//

#import "SeyahatCell.h"
#import "NCollectionCellBackgroundView.h"
@implementation SeyahatCell

- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //self.backgroundColor = [UIColor whiteColor];


        PNCollectionCellBackgroundView* backgroundView = 
          [[PNCollectionCellBackgroundView alloc] initWithFrame:frame];
        self.backgroundView = backgroundView;
        self.imageView = 
          [[UIImageView alloc] initWithFrame:CGRectMake(5, 
                                                        5, 
                                                        frame.size.width-10, 
                                                        frame.size.height-10)];
        [self.contentView addSubview:self.imageView];
    }
    return self;
}

还有我的 cellForItemAtIndexPath 方法;

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView
                  cellForItemAtIndexPathNSIndexPath *)indexPath
{

    SeyahatCell *seyahatCell =
    [collectionView dequeueReusableCellWithReuseIdentifier:SeyahatCellIdentifier    
                                              forIndexPath:indexPath];


    Post * p = [self.posts objectAtIndex:indexPath.row];
    [seyahatCell.imageView setImage:nil];
    if(p.thumbnail){
        [seyahatCell.imageView setImageWithURL:[NSURL URLWithString:p.thumbnail]];
    }
    else{
        [seyahatCell.imageView setImage:nil];
    }

    return seyahatCell;
}

我该如何解决这个问题?



Best Answer-推荐答案


尝试使用现有的布局解决方案。为此,我更喜欢使用 https://github.com/bryceredd/RFQuiltLayout

关于ios - 自定义 UICollectionViewLayout 不同的单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16934781/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap