2013-06-05 177 views
1

我有一個具有2種不同類型的UICollectionViewCells的UICollectionView。有一個選項卡提示顯示每種類型的單元格,並且一次只能看到一種類型的單元格。UICollectionView對齊單元格

出於某種原因,1種單元格在UICollectionView中左對齊,但另一個單元格在中心水平對齊。我希望兩種類型的單元格都左對齊。

邊緣插圖是:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{ 
    return UIEdgeInsetsMake(10,10,10,10); 
} 

任何幫助將非常感激。

回答

2

根據它們的大小,單元格的佈局方式會有所不同,因此您需要爲每種單元格類型使用不同的插入。我想應該是可行的這樣的:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{ 
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]; 
    if ([cell isKindOfClass:[MyClass1 class]]){ 
     return UIEdgeInsetsMake(10,10,10,10); 
    }else{ 
     return UIEdgeInsetsMake(20,10,10,10); 
} 

替換你的正確的類名MyClass1的和實驗與邊緣插圖,直到你得到你想要的行爲。

+0

謝謝,這是一個愚蠢的錯誤。我沒有意識到這一點,但是居中的單元格太寬,不能有一個以上的單元格。我已經減小了單元的大小並解決了問題。 – user2312407