SORU
22 Kasım 2012, PERŞEMBE


Nasıl Ek Görüntüleyin Bölüm Başlıkları gibi UİTableView düz tarzı UİCollectionView içinde yüzer yapmak

Etkisi "UICollectionView. kayan bölümünde bir başlık elde etmek için mücadele ediyorum UITableView yeterince kolay bir şey (UITableViewStylePlain varsayılan davranış) UICollectionView imkansız zor iş bir sürü olmadan görünüyor. Bariz mi kaçırdım?

Apple bunu başarmak için nasıl bir Dokümantasyon sağlar. UICollectionViewLayout alt ve sadece bu etkiyi elde etmek için özel bir düzen kurması gerekiyor gibi görünüyor. Bu iş biraz, aşağıdaki yöntemi kullanmayı gerektirir:

Geçersiz kılmak için yöntemler

Her düzen nesnesi aşağıdaki yöntemleri uygulamak gerekir:

collectionViewContentSize
layoutAttributesForElementsInRect:
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath: (if your layout supports supplementary views)
layoutAttributesForDecorationViewOfKind:atIndexPath: (if your layout supports decoration views)
shouldInvalidateLayoutForBoundsChange:

Ancak ek görünüm hücreleri üzerinde yüzer yapmak için nasıl bana açıklamaz ve "sopa" için bir sonraki bölüme kadar görünüm tepesine ulaştı. Düzen Özellikleri, Bu bir bayrak var mı?

UITableView kullanacaktım ama kolayca bir görünüm koleksiyonu ile elde edilir koleksiyon oldukça karmaşık bir hiyerarşi oluşturmak için ihtiyacım var.

Rehberlik veya herhangi bir örnek kod büyük mutluluk duyacağız!

CEVAP
3 Aralık 2012, PAZARTESİ


Aşağıdaki temsilci yöntemleri uygulamak:

– collectionView:layout:sizeForItemAtIndexPath:
– collectionView:layout:insetForSectionAtIndex:
– collectionView:layout:minimumLineSpacingForSectionAtIndex:
– collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
– collectionView:layout:referenceSizeForHeaderInSection:
– collectionView:layout:referenceSizeForFooterInSection:

Sizin görüşünüze göre kumanda :cellForItemAtIndexPath yöntem var (sadece doğru değerler verir). Ya da, temsilci yöntemleri kullanmak yerine, aynı zamanda bu değerler, doğrudan düzen nesne, örneğin [layout setItemSize:size]; içinde ayarlayabilirsiniz.

Bu yöntemlerden birini kullanarak Özel bir Düzen ayarladığınızda kaldırıldı onlar gibi İB yerine Kod ayarlarınızı ayarlamanızı sağlar. Senin için <UICollectionViewDelegateFlowLayout> eklemeyi unutmayın .h dosyası da!

UICollectionViewFlowLayout, nasıl istersen öyle olsun, ve H dosyası olduğundan emin olun: yeni bir Alt sınıf oluşturun

#import <UIKit/UIKit.h>

@interface YourSubclassNameHere : UICollectionViewFlowLayout

@end

İçinde Uygulanması aşağıdakilerden emin olun Dosya:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {

    NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    UICollectionView * const cv = self.collectionView;
    CGPoint const contentOffset = cv.contentOffset;

    NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
        if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
            [missingSections addIndex:layoutAttributes.indexPath.section];
        }
    }
    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
            [missingSections removeIndex:layoutAttributes.indexPath.section];
        }
    }

    [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];

        UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];

        [answer addObject:layoutAttributes];

    }];

    for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {

        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {

            NSInteger section = layoutAttributes.indexPath.section;
            NSInteger numberOfItemsInSection = [cv numberOfItemsInSection:section];

            NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
            NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];

            NSIndexPath *firstObjectIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
            NSIndexPath *lastObjectIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];

            UICollectionViewLayoutAttributes *firstObjectAttrs;
            UICollectionViewLayoutAttributes *lastObjectAttrs;

            if (numberOfItemsInSection > 0) {
                firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
                lastObjectAttrs = [self layoutAttributesForItemAtIndexPath:lastObjectIndexPath];
            } else {
                firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                    atIndexPath:firstObjectIndexPath];
                lastObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                                   atIndexPath:lastObjectIndexPath];
            }

            CGFloat headerHeight = CGRectGetHeight(layoutAttributes.frame);
            CGPoint origin = layoutAttributes.frame.origin;
            origin.y = MIN(
                           MAX(
                               contentOffset.y   cv.contentInset.top,
                               (CGRectGetMinY(firstObjectAttrs.frame) - headerHeight)
                               ),
                           (CGRectGetMaxY(lastObjectAttrs.frame) - headerHeight)
                           );

            layoutAttributes.zIndex = 1024;
            layoutAttributes.frame = (CGRect){
                .origin = origin,
                .size = layoutAttributes.frame.size
            };

        }

    }

    return answer;

}

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {

    return YES;

}

"Akış Düzeni için Arayüz Oluşturucu, o "yarattı." Sınıf YourSubclassNameHere seçin "Özel seçim Ve Çalıştırın!

(Not: contentİnset almayabilir yukarıdaki kodu.alt değerleri, ya da özellikle büyük ya da küçük altbilgi nesneleri veya 0 nesne koleksiyonları ama hiçbir altbilgi.)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BurnedInDotCom

    BurnedInDotC

    3 NİSAN 2010
  • The Onion

    The Onion

    14 Mart 2006
  • Truc Minh

    Truc Minh

    23 Ocak 2011