可以在空的UICollectionView中显示至少一个装饰视图吗?

时间:2014-01-20 20:24:43

标签: uicollectionview uicollectionviewlayout

好吧,我有一个自定义UICollection,它是一个书架。 即使我的对象数组是空的(书籍封面),我也想展示至少一个“空”的架子。

怎么可能?

谢谢!


[更新] 我的解决方案:

也许它不是最好的方式,但对我来说就像一个魅力。我试图验证在我的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
    }
}

1 个答案:

答案 0 :(得分:-2)

您必须继承UICollectionViewFlowLayout并覆盖layoutAttributesForElementsInRect:。然后在该方法中,调用super,并将装饰视图布局属性添加到数组中。