1

我正在尝试使用 UICollectionView 和自定义布局来实现类似于本机日历应用程序的时间线视图。我是新手。

这就是我的问题的来源。

我正在使用装饰视图来实现那些背景网格线,并尝试使用补充视图来制作时间标签(靠近网格线),并将使用 Cell 来制作事件,但还没有那么远。

但是在做这些事件之前,我发现当我运行它时,所有的补充视图都不起作用,不管我有没有单元格。我发现我的collectionView:viewForSupplementaryElementOfKind:atIndexPath: method is not called

所以我想知道如何以及何时调用此方法?什么可能导致我的情况没有被调用?

其实,做那些带有补充视图的时间标签好不好?我不确定,因为即使没有事件(部分中没有单元格/项目),我也确实需要它们可见。

这是我的代码:

视图控制器

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor whiteColor];

    [self.collectionView registerClass:TodayCellKindTask.class forCellWithReuseIdentifier:CellKindTaskIdentifier];
    [self.collectionView registerClass:TodayTimelineTimeHeader.class forSupplementaryViewOfKind:TimelineKindTimeHeader withReuseIdentifier:TimelineTimeHeaderIdentifier];
    [self.timelineViewLayout registerClass:TodayTimelineTileWhole.class forDecorationViewOfKind:TimelineKindTileWholeHour];
    [self.timelineViewLayout registerClass:TodayTimelineTileHalf.class forDecorationViewOfKind:TimelineKindTileHalfHour];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TodayCellKindTask *cellTask = [collectionView dequeueReusableCellWithReuseIdentifier:CellKindTaskIdentifier forIndexPath:indexPath];
    return cellTask;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"viewForSupplementaryElementOfKind");
    TodayTimelineTimeHeader *timeHeader = [self.collectionView dequeueReusableSupplementaryViewOfKind:TimelineKindTimeHeader withReuseIdentifier:TimelineTimeHeaderIdentifier forIndexPath:indexPath];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *today = [NSDate date];
    NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:today];
    [comps setHour:indexPath.item];
    timeHeader.time = [calendar dateFromComponents:comps];
    return timeHeader;
}

自定义布局

// in prepareLayout
NSMutableDictionary *timeHeaderAttributes = [NSMutableDictionary dictionary];
CGSize headerSize = [TodayTimelineTimeHeader defaultSize];
CGFloat headerOffsetY = (tileSize.height - headerSize.height) / 2;
for (NSInteger hour = 24; hour >= 0; hour--) {
    NSIndexPath *timeHeaderIndexPath = [NSIndexPath indexPathForItem:hour inSection:0];
    UICollectionViewLayoutAttributes *currentTimeHeaderAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:TimelineKindTimeHeader withIndexPath:timeHeaderIndexPath];
    CGFloat headerPosY = hour * 2 * tileSize.height + headerOffsetY;
    currentTimeHeaderAttributes.frame = CGRectMake(TimeHeaderPosX, headerPosY, headerSize.width, headerSize.height);
    timeHeaderAttributes[timeHeaderIndexPath] = currentTimeHeaderAttributes;
}
self.timelineTileAttributes[TimelineKindTimeHeader] = timeHeaderAttributes;


// layoutAttributesForSupplementaryViewOfKind
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    return self.timelineTileAttributes[kind][indexPath];
}

// layoutAttributesForElementsInRect
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.timelineTileAttributes.count];
    [self.timelineTileAttributes enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
                                                                    NSDictionary *elementsInfo,
                                                                    BOOL *stop) {
       [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
                                                        UICollectionViewLayoutAttributes *attributes,
                                                        BOOL *stop) {
           if (CGRectIntersectsRect(rect, attributes.frame)) {
               [allAttributes addObject:attributes];
           }
       }];
    }];
    return allAttributes;
}

// collectionViewContentSize
- (CGSize)collectionViewContentSize {
    CGSize tileSize = [TodayTimelineTileWhole defaultSize];
    CGFloat contentHeight = tileSize.height * self.numberOfTiles;
    return CGSizeMake(tileSize.width, contentHeight);
}

我尽量不在这里发布所有代码,因为那会很多,但是如果您需要了解其他人,请告诉我。

任何提示表示赞赏!

松树

4

1 回答 1

2

所以,这个问题解决了,原因是我的愚蠢错误,将这些视图的位置 x 设置在屏幕之外,因此没有调用该方法。没有其他的。

我通过在prepareLayout中注销每个视图的框架找到了这个原因,并发现x位置是错误的。我从视网膜设计中得到了这个位置,所以......</p>

我使用了补充视图,它可以正常工作。

我的教训:别担心,冷静下来。这可以节省您犯错的时间。

于 2013-06-19T08:01:24.607 回答