13

UICollectionViewLayoutAttributes クラスをサブクラス化し、いくつかのカスタム プロパティを追加しました。

私のカスタム UICollectionViewLayout クラスでは、静的 + (Class)layoutAttributesClass をオーバーライドして、新しい属性クラスを返します。

私の UICollectionViewLayout クラスでは、 をオーバーライドし-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect、値をカスタム プロパティに設定します。

そこで属性クラスを調べると、カスタム プロパティが適切に設定されていることがわかります。

最後に、UICollectionViewCell でこれらのプロパティを取得する必要があるため-(void) applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes、カスタム UICollectionViewLayoutAttributes クラスの値を取得するためにオーバーライドしますが、値は nil です。まるで私がそれらを設定したことがないかのように。

変換などを含め、残りのすべての属性は完全に機能しています。明らかに、私は何か間違ったことをしています。お知らせ下さい。

私のカスタムクラスのHeaderFileが含まれています

@interface UICollectionViewLayoutAttributesWithColor : UICollectionViewLayoutAttributes

@property (strong,nonatomic) UIColor *color;

@end

これが実装です。ご覧のとおり、特別なことは何もありません

@implementation UICollectionViewLayoutAttributesWithColor

@synthesize color=_color;

@end
4

3 に答える 3

27

答えは簡単です。UICollectionViewAttributes は NSCopying プロトコルを実装しているため、 copyWithZone: をオーバーライドするだけです。Apple コードはカスタム属性オブジェクトのコピーを作成していますが、copyWithZone を実装していないため、カスタム属性は新しいオブジェクトにコピーされていません。必要な作業の例を次に示します。

@interface IRTableCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes {
    CGRect _textFieldFrame;
}

@property (nonatomic, assign) CGRect  textFieldFrame;

@end

そして実装:

- (id)copyWithZone:(NSZone *)zone
{
    IRTableCollectionViewLayoutAttributes *newAttributes = [super copyWithZone:zone];
    newAttributes->_textFieldFrame = _textFieldFrame;

    return newAttributes;
}
于 2012-10-18T17:59:52.310 に答える
1

カスタム値の削除の問題は、-copyWithZoneを実装する必要があるためです。UICollectionViewLayoutAttributesがNSCopyingを実装して使用するためです。

于 2012-10-18T18:11:27.960 に答える
0

レイアウト属性をセルに適用しようとしているようです。次のように実行できます。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Get Cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    //Apply Attributes
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    [cell applyLayoutAttributes:attributes];

    return cell;
}

ただし、UICollectionView の実装では、UICollectionViewLayoutAttributes のサブクラスではなく、UICollectionViewCell のサブクラスにカスタム プロパティを追加しました。ほとんどの場合、UICollectionViewCell をサブクラス化する方が効率的だと思います。

@interface ColoredCollectionCell : UICollectionViewCell
@property (strong,nonatomic) UIColor *color;
@end

@implementation ColoredCollectionCell
// No need to @synthesize in iOS6 sdk
@end

コレクション ビュー コントローラーで:

- (UICollectionViewCell*)collectionView:(UICollectionView*)cv cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    //Get the Cell
    ColoredCollectionCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    //Get the color from Array of colors
    UIColor *thisColor = self.colorsArray[indexPath.item];

    //Set cell color
    cell.color = thisColor;

    return cell;

}
于 2012-10-10T02:04:24.783 に答える