3

所以基本上我的問題是我的uicollectionview的單元格是從上到下排列的,而不是從左到右。UICollectionView - 水平排列單元格

This is what it looks like - 
[1][4][7] 
[2][5][8] 
[3][6][9] 

This is what i want - 
[1][2][3] 
[4][5][6] 
[7][8][9] 

另一個問題是當我滾動水平,而不是移動頁面完整的320點。它只能移動到適合視圖中的下一個單元格。

this is what it looks like - 
    [2][3][10] 
    [5][6][ ] 
    [8][9][ ] 

this is what i want - 
    [10][ ][ ] 
    [ ][ ][ ] 
    [ ][ ][ ] 

我希望水平渲染它會修復它。我仍然對ios很陌生,所以請告訴我如何去做,我搜索的所有內容都不是針對我的確切問題,或者是過時的。

+0

嗨,你有沒有得到一個解決方案。 – Bonnie 2015-03-16 07:33:27

+0

你好,你找到一個解決方案嗎? – 2016-04-09 21:49:13

回答

1

1)如果在集合視圖中使用橫向滾動,訂貨會喜歡你有什麼

[1][4][7] 
[2][5][8] 
[3][6][9] 

如果你想要這個爲了改變,你必須使用垂直滾動。

2)如果你想收集以滾動一整頁,從IB啓用分頁或

colloctionView.pagingEnabled = YES; 
+0

是否有反正我可以有單元格渲染和水平滾動就像跳板?我有分頁啓用時,我滾動到右側它只是足夠遠,以適應屏幕上的屏幕單元仍然 – user3296487 2015-02-08 02:37:33

+0

不,有與uicollection視圖沒有辦法 – 2015-02-08 03:25:59

2

這是我的解決方案,希望有所幫助:

CELLS_PER_ROW = 4; 
CELLS_PER_COLUMN = 5; 
CELLS_PER_PAGE = CELLS_PER_ROW * CELLS_PER_COLUMN; 

UICollectionViewFlowLayout* flowLayout = (UICollectionViewFlowLayout*)collectionView.collectionViewLayout; 
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 
flowLayout.itemSize = new CGSize (size.width/CELLS_PER_ROW - delta, size.height/CELLS_PER_COLUMN - delta); 
flowLayout.minimumInteritemSpacing = ..; 
flowLayout.minimumLineSpacing = ..; 
.. 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
        cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSInteger index = indexPath.row; 
    NSInteger page = index/CELLS_PER_PAGE; 
    NSInteger indexInPage = index - page * CELLS_PER_PAGE; 
    NSInteger row = indexInPage % CELLS_PER_COLUMN; 
    NSInteger column = indexInPage/CELLS_PER_COLUMN; 

    NSInteger dataIndex = row * CELLS_PER_ROW + column + page * CELLS_PER_PAGE; 

    id cellData = _data[dataIndex]; 
    ... 
} 
0

您必須實現CustomLayout來解決這個問題。這裏是我的解決方案:

/* 
    This is what it looks like - 
    [1][3] [5][7] 
    [2][4] [6][8] 

    This is what i want - 
    [1][2] [5][6] 
    [3][4] [7][8] 
*/ 

#import "AWCustomFlowLayout.h" 

CGFloat const kCellWidth = 130.0; 
CGFloat const kCellHeight = 170.0; 
CGFloat const kCellLineSpacing = 20.0; 
CGFloat const kCellInteritemSpacing = 10.0; 

@implementation AWCustomFlowLayout 

- (instancetype)init { 
    self = [super init]; 
    if (self) { 
     self.minimumLineSpacing = kCellLineSpacing; 
     self.minimumInteritemSpacing = kCellInteritemSpacing; 
     self.itemSize = CGSizeMake(kCellWidth, kCellHeight); 
     self.scrollDirection = UICollectionViewScrollDirectionHorizontal; 
     self.sectionInset = UIEdgeInsetsMake(0, kCellLineSpacing, 0, 0); 
    } 

    return self; 
} 

- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath]; 
    [self modifyLayoutAttribute:attribute]; 
    return attribute; 
} 

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{ 
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; 
    for(UICollectionViewLayoutAttributes *attribute in attributes){ 
     [self modifyLayoutAttribute:attribute]; 
    } 

    return attributes; 
} 


- (void)modifyLayoutAttribute:(UICollectionViewLayoutAttributes*)attribute{ 
    CGRect frame = attribute.frame; 

    NSInteger cellX = attribute.frame.origin.x; 
    NSInteger cellY = attribute.frame.origin.y; 
    NSInteger cellWidth = attribute.frame.size.width; 
    NSInteger const firstColumnX = kCellLineSpacing; 
    NSInteger const secondColumnX = kCellWidth + kCellLineSpacing*2; 
    NSInteger const firstRowY = 0; 
    NSInteger const secondRowY = kCellHeight + kCellInteritemSpacing; 

    BOOL isFirstColumn = cellX % firstColumnX == 0; 
    BOOL isSecondColumn = (cellX+cellWidth) % (secondColumnX+cellWidth) == 0; 
    BOOL isFirstRow = cellY == firstRowY; 
    BOOL isSecondRow = cellY == secondRowY; 

    if (isFirstColumn && isSecondRow) { 
     frame.origin.x += secondColumnX-firstColumnX; 
     frame.origin.y = firstRowY; 
    } 
    else if (isSecondColumn && isFirstRow) { 
     frame.origin.x -= secondColumnX-firstColumnX; 
     frame.origin.y = secondRowY; 
    } 

    attribute.frame = frame; 
} 

@end 
相關問題