首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义NSCollectionViewAttributes子类不能正确应用NSFlowLayout

自定义NSCollectionViewAttributes子类不能正确应用NSFlowLayout
EN

Stack Overflow用户
提问于 2022-02-14 13:54:25
回答 1查看 45关注 0票数 0

我目前正在开发一个带有自定义布局的NSCollectionView。为此,我对NSCollectionViewFlowLayout子类进行了子类,以对齐我的项(所有这些项都有固定的宽度,这使得算法变得非常容易)。我现在遇到的问题是,只有我的集合视图中的前九项被正确显示,即是对齐的。

这些前九项在最初显示集合视图时至少部分可见。在向下滚动集合视图时出现的其他项将在没有任何顶对齐的情况下绘制,只是作为NSCollectionViewFlowLayout的默认行为垂直地以每一行为中心。通过广泛的日志记录,我可以验证我的布局只提供了正确修改的NSCollectionViewLayoutAttributes,以便进行顶对齐。

当我调整包含NSCollectionView的窗口的大小,从而调整集合视图本身的大小时,它的可见部分中的所有项都会突然被正确显示。

我遗漏了什么?

下面是我的自定义子类:

代码语言:javascript
复制
#import "MyCollectionViewLayout.h"

@interface MyCollectionViewLayout ()

@property (nonatomic, strong) NSMutableDictionary<NSIndexPath *, NSCollectionViewLayoutAttributes *> *attributesCache;
@property NSInteger numberOfItemsPerRow;
@property NSInteger numberOfItemsTotal;

@end


@implementation MyCollectionViewLayout

- (void)prepareLayout {
    NSLog(@"Preparing layout");
    [super prepareLayout];
    self.itemSize = CGSizeMake(100, 138);
    self.minimumInteritemSpacing = 5;
    self.minimumLineSpacing = 5;
    self.sectionInset = NSEdgeInsetsMake(10, 10, 10, 10);
    self.attributesCache = @{}.mutableCopy;
    self.numberOfItemsTotal = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];
    
    NSInteger numberOfItemsPerRow = 1;
    CGFloat computedSize = self.itemSize.width;
    CGFloat usableWidth = self.collectionView.frame.size.width - (self.sectionInset.right + self.sectionInset.left);
    repeat: {
        computedSize += self.minimumInteritemSpacing + self.itemSize.width;
        if (computedSize < usableWidth) {
            numberOfItemsPerRow++;
            goto repeat;
        }
    }
    self.numberOfItemsPerRow = numberOfItemsPerRow;
}

#pragma mark NSCollectionViewFlowLayout override

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSLog(@"Getting layout attributes for rect: %f, %f, %f, %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    NSArray *attributesArray = [super layoutAttributesForElementsInRect:rect].mutableCopy;
    for (int i = 0; i < attributesArray.count; i++) {
        NSCollectionViewLayoutAttributes *attributes = attributesArray[i];
        NSLog(@"Forwarding attribute request for %@", attributes.indexPath);
        attributes = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
    }
    return attributesArray;
}

- (NSCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.attributesCache[indexPath]) NSLog(@"");
    NSLog(@"Getting layout attributes for %@", indexPath);
    if (!self.attributesCache[indexPath]) {
        NSLog(@ "Not cached yet, caching full row");
        [self computeAndCacheAttributesForRowContaining:indexPath];
    }
    return self.attributesCache[indexPath];
}

#pragma mark Private instance methods

- (void)computeAndCacheAttributesForRowContaining:(NSIndexPath *)indexPath {
    NSDictionary<NSIndexPath *, NSCollectionViewLayoutAttributes *> *allAttributesInRowByPath = [self getAllAttributesInRowContaining:indexPath];
    CGFloat minY = CGFLOAT_MAX;
    for (NSIndexPath *path in allAttributesInRowByPath) {
        if (allAttributesInRowByPath[path].frame.origin.y < minY) {
            minY = allAttributesInRowByPath[path].frame.origin.y;
        }
    }
    for (NSIndexPath *path in allAttributesInRowByPath) {
        if (indexPath.item == 9) {
            
        }
        NSLog(@"Changing frame for indexPath %@", path);
        NSRect frame = allAttributesInRowByPath[path].frame;
        NSLog(@"Previous y Position: %f", frame.origin.y);
        NSLog(@"New y Position:      %f", minY);
        frame.origin.y = minY;
        allAttributesInRowByPath[path].frame = frame;
    }
    [self.attributesCache addEntriesFromDictionary:allAttributesInRowByPath];
}

- (NSDictionary<NSIndexPath *, NSCollectionViewLayoutAttributes *> *)getAllAttributesInRowContaining:(NSIndexPath *)indexPath {
    NSMutableDictionary<NSIndexPath *, NSCollectionViewLayoutAttributes *> *attributesToReturn = @{}.mutableCopy;
    NSInteger index = indexPath.item;
    NSInteger firstIndex = index - (index % self.numberOfItemsPerRow);
    NSIndexPath *path;
    for (index = firstIndex; (index < firstIndex + self.numberOfItemsPerRow) && (index < self.numberOfItemsTotal); index++) {
        path = [NSIndexPath indexPathForItem:index inSection:indexPath.section];
        [attributesToReturn setObject:[super layoutAttributesForItemAtIndexPath:path].copy forKey:path];
    }
    return attributesToReturn.copy;
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-14 15:00:38

您丢失了attributesArray[i] = attributes;中的layoutAttributesForElementsInRect :并返回未修改的属性。

代码语言:javascript
复制
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSLog(@"Getting layout attributes for rect: %f, %f, %f, %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    NSMutableArray *attributesArray = [super layoutAttributesForElementsInRect:rect].mutableCopy;
    for (int i = 0; i < attributesArray.count; i++) {
        NSCollectionViewLayoutAttributes *attributes = attributesArray[i];
        NSLog(@"Forwarding attribute request for %@", attributes.indexPath);
        attributes = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
        attributesArray[i] = attributes; // <---
    }
    return attributesArray;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71113012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档