2014-01-20 26 views
2

好吧,我有一個自定義的UICollection是一個書架。 即使我的對象數組是空的(書的封面),我想至少展示一個「空」的書架。可以在空的UICollectionView中至少顯示一個裝飾視圖?

怎麼可能?

謝謝!


[更新]我的解決辦法:

也許它不是最好的方式,但對我來說就像一個魅力。我嘗試驗證在我的UICollectionReusableView類中是否有一些方法可以做到這一點,但我沒有成功。所以,我嘗試了另一種使用collectionView方法的方式。

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 
    //even the array of elements was empty, return at least 1 fake book 
    if(_books.count == 0) 
     return 1; 
    else 
     return _books.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; { 

    CellCollection *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CUSTOM_CELL" forIndexPath:indexPath]; 
    [cell setHidden:YES]; // not show any items by default 

    // If exist elements 
    if(_books.count != 0){ 
     [cell setHidden:NO]; // shows the items 

     ..... // config the cell properties 
     ....... 
    } 

    return cell; 
} 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    // only if the selected element exist 
    if(_books[indexPath.row]){ 

     // do something 
    } 
} 

回答

-2

你必須繼承UICollectionViewFlowLayout並覆蓋layoutAttributesForElementsInRect:。然後在該方法中,調用super,並將裝飾視圖佈局屬性添加到數組中。

+0

這是非常不確定的。請添加支持代碼和詳細信息。 – Andrew

+0

我認爲這已經夠詳細了。如果OP無法工作,那麼我很樂意提供幫助。 SO不是關於吐出代碼,而是關於提供幫助。 –

+1

我會爭辯說,SO是爲了向每個人提供幫助,而不僅僅是向OP提供幫助。我自己也有同樣的問題,你的回答沒有提供足夠的細節來幫助我,所以我猜其他人可能會遇到同樣的問題。在你的防守中,很難對一個不太詳細的問題給出詳細的答案。 – Andrew

相關問題