代码之家  ›  专栏  ›  技术社区  ›  jo3birdtalk

如何显示每行3个单元格UICollectionView

  •  0
  • jo3birdtalk  · 技术社区  · 10 年前

    根据标题,我使用UICollectionView显示图像。我需要每行显示3个单元格,类似于 Instagram 这样做。

    因为有很多屏幕尺寸,我得到了iPhone屏幕的宽度,然后将其除以3。然而,它并没有按照我的需要显示。

    这是我的密码 viewDidLoad() 方法:

    private let leftAndRightPaddings: CGFloat = 8.0
    private let numberOfItemsPerRow: CGFloat = 3.0
    private let heightAdjustment: CGFloat = 30.0    
    
    let bounds = UIScreen.mainScreen().bounds
    let width = (bounds.size.width - leftAndRightPaddings) / numberOfItemsPerRow
    let layout = userDetailCollection.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = CGSizeMake(width, width)
    

    enter image description here

    3 回复  |  直到 8 年前
        1
  •  4
  •   Rohit Pradhan praveen kumar    9 年前

    替换此行

    let width = (bounds.size.width - leftAndRightPaddings) / numberOfItemsPerRow
    

    具有

    let width = (bounds.size.width - leftAndRightPaddings*4) / numberOfItemsPerRow
    

    由于您没有考虑两个项目之间的间距&右侧插图 间距,因此不会在屏幕中调整。

    private let leftAndRightPaddings: CGFloat = 15.0
    private let numberOfItemsPerRow: CGFloat = 3.0
    
    let bounds = UIScreen.mainScreen().bounds
    let width = (bounds.size.width - leftAndRightPaddings*(numberOfItemsPerRow+1)) / numberOfItemsPerRow
    let layout = userDetailCollection.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = CGSizeMake(width, width)   
    

    尝试此代码

        2
  •  2
  •   HeTzi    10 年前

    最重要的 UICollectionViewLayout 将使您在创建collectionViews时更加灵活。下面是的一个片段 YourOwnCollectionViewLayout : UICollectionViewLayout :

    - (NSInteger)itemWidth {
        return (self.collectionView.bounds.size.width - (HORIZONTAL_PAD * (NUMBER_OF_ITEMS_PER_ROW+1))) / NUMBER_OF_ITEMS_PER_ROW; // pad from each side + pad in the middle
    }
    
    - (NSInteger)itemHeight {
        return self.itemWidth; // or something else
    }
    
    - (NSString *)keyForIndexPath:(NSIndexPath *)indexPath {
        NSString *retVal = [NSString stringWithFormat:@"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
        return retVal;
    }
    
    - (CGRect)frameForItemAtIndexPath:(NSIndexPath *)indexPath {
        CGFloat itemWidth  = [self itemWidth];
        CGFloat itemHeight = [self itemHeight];
    
        CGFloat originX = HORIZONTAL_PAD + ((indexPath.row % NUMBER_OF_ITEMS_PER_ROW)*(HORIZONTAL_PAD + itemWidth));
        CGFloat originY = VERTICAL_PAD + (floorf(indexPath.row/NUMBER_OF_ITEMS_PER_ROW)*(VERTICAL_PAD + itemHeight));
    
        return CGRectMake(originX, originY, itemWidth, itemHeight);
    }
    
    - (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];
    
                UICollectionViewLayoutAttributes *itemAttributes =
                [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                itemAttributes.frame = [self frameForItemAtIndexPath:indexPath];
    
                NSString *key        = [self keyForIndexPath:indexPath];
                cellLayoutInfo[key]  = itemAttributes;
            }
        }
    
        newLayoutInfo[@"CellKind"] = cellLayoutInfo;
        self.layoutInfo            = newLayoutInfo;
    }
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
        NSString *key = [self keyForIndexPath:indexPath];
        UICollectionViewLayoutAttributes *retVal = self.layoutInfo[@"CellKind"][key];
        return retVal;
    }
    
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
        NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];
    
        [self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
                                                             NSDictionary *elementsInfo,
                                                             BOOL *stop) {
            [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
                                                              UICollectionViewLayoutAttributes *attributes,
                                                              BOOL *innerStop) {
    
                if (CGRectIntersectsRect(rect, attributes.frame)) {
                    [allAttributes addObject:attributes];
                }
            }];
        }];
    
        return allAttributes;
    }
    
    - (CGSize)collectionViewContentSize {
        return CGSizeMake(self.collectionView.bounds.size.width, floorf([self.collectionView numberOfItemsInSection:0]/NUMBER_OF_ITEMS_PER_ROW) * (self.itemHeight + VERTICAL_PAD) + VERTICAL_PAD);
    }
    

    在主视图中添加: [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[[YourOwnCollectionViewLayout alloc] init]]; 你就完蛋了!

        3
  •  0
  •   Varun    10 年前

    你没有考虑 sectionInset interItemSpacing 。它有一些默认值,用于推动单元格(提供填充)。您可以设置 项目间间距 到0和 部分插入 UIEdgeInsetsZero 或者在计算cellWidth时必须考虑它们的值。

    覆盖这些用于设置大小、插入和interItem间距的协议方法

    optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    
    optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
    
    optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
    
    optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
    
    推荐文章