簡體   English   中英

使用 iOS UICollectionView 僅允許某些部分的標題視圖

[英]Allow A Header View for Only Certain Sections Using an iOS UICollectionView

下面的代碼正確顯示了我的標題視圖,但是對於 UICollectionView 中的每個部分:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * headerView =
        [collectionView 
            dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                               withReuseIdentifier:@"SectionHeaderCollectionReusableView"
                                      forIndexPath:indexPath];
    switch (indexPath.section) {
        case Section_One:
            return headerView;
        case Section_Two:
            return headerView;
        case Section_Three:
            return headerView;
        case Section_Four:
            return headerView;
        case Section_Five:
            return headerView;

        default:
            return headerView;
    }
}

我想做的不是顯示“Section_One”或“Section_Two”的標題視圖,而是返回“nil”會導致“NSInternalInconsistencyException”:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * headerView =
        [collectionView 
            dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                               withReuseIdentifier:@"SectionHeaderCollectionReusableView"
                                      forIndexPath:indexPath];
    switch (indexPath.section) {
        case Section_One:
            return nil;
        case Section_Two:
            return nil;
        case Section_Three:
            return headerView;
        case Section_Four:
            return headerView;
        case Section_Five:
            return headerView;

        default:
            return nil;
    }
}

我需要做什么才能僅顯示某些部分的標題視圖?

繼續並為每個部分返回一個標題,然后在此 UICollectionViewDelegate 函數中將部分標題的大小設置為零。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeZero;
    }else {
        return CGSizeMake(self.collectionView.bounds.size.width, desiredHeight);
    }
}

我有一個案例,一個UICollectionViewController控制兩個UICollectionView (稍后稱為集合視圖 1 和 2),我想要第一個的標題,第二個沒有標題(或頁腳)。

@mwright 的回答中缺少的是,當您為集合視圖 2返回CGSizeZero時,如下所示:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if collectionView == self.collectionView2 {
        return CGSizeZero
    }
    return < something else >
}

...意味着collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView根本沒有被調用collection view 2

這意味着您不必擔心會徒勞地為第二個集合視圖返回“錯誤”標題。

我注意到當您在 XIB 中使用 AutoLayout 作為可重用標頭時,接受的答案中斷了。

如果您將內容與邊緣隔開,或者將標題視圖中的項目設置為靜態的、不可變的大小,則它尤其被破壞。 將標頭大小設置為 CGSizeZero 使我的調試器控制台變得混亂,其中包含來自 Interface Builder 的數十條警告,說它們會打破所有約束以滿足委托方法中設置的要求。

雖然這本身在技術上並不是一場災難,但它仍然很臟。 在 Swift 和 AutoLayout 時代,必須有一個更清潔的解決方案。 此外,您永遠不想在工作時將這種東西運送給客戶。

為了解決這個問題,我不只是調用referenceSizeForHeaderInSection:並返回CGSizeZero ,而是使用 XIB 創建了UICollectionReusableView的另一個子類,並將其中的視圖高度設置為0

然后,我在viewForSupplementaryElementOfKind方法中包含的switch語句之外將該變體出列。 這滿足了 Interface Builder視覺要求! 🎉

無論如何,在您調試時,Beats 會在控制台中打印數百個無法滿足的約束警告。

如果返回大小為 (0, 0) 的值,則不會添加任何標題。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

 switch section {
 case 0:
   return CGSize(width: collectionView.bounds.width, height: 70)
 case 1:
   return CGSize(width: 0, height: 0) // NO HEADER WILL BE ADDED
 case 2:
   return CGSize(width: collectionView.bounds.width, height: 70)
 case 3:
   return CGSize(width: 0, height: 0) // NO HEADER WILL BE ADDED
 default:
   return CGSize(width: collectionView.bounds.width, height: 70)
 }

}
 

嘗試返回一個空視圖。 可能是更好的方法,但這可以工作....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM