首页 \ 问答 \ 插入到包含页脚的UICollectionView部分的问题(Issues inserting into UICollectionView section which contains a footer)

插入到包含页脚的UICollectionView部分的问题(Issues inserting into UICollectionView section which contains a footer)

我有一个典型的UICollectionView,它以垂直方式使用UICollectionViewFlowLayout。 我正在使用分页API来填充集合视图。 为了触发下一页下载,我在使用委托时要求页脚布局:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    RDLoadMoreReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"RDLoadMoreReusableView" forIndexPath:indexPath];
    view.delegate = self;
    [view start];
    [self loadNextPageOfAssets];
    return view;
}

这是我的loadNextPageOfAssets背后的代码:

-(void)loadNextPageOfAssets{
    __weak RDRadiusGridViewController *weakSelf = self;

    // Increment page and request assets. They are added to cluster.assets before the completion block is called.
    self.cluster.pagination.page++;
    [self.cluster requestAssetsWithCompletionBlock:^(NSArray *assets) {

        SM_LOG_DEBUG(@"page.total: %ld assets.count: %ld", self.cluster.pagination.totalCount, (long)assets.count);

        NSMutableArray *indexPaths = [[NSMutableArray alloc]initWithCapacity:assets.count];
        for(NSUInteger index = 0; index < assets.count; index++){
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.cluster.assets.count - assets.count + index inSection:0];
            SM_LOG_DEBUG(@"Inserting indexPath: %ld:%ld", (long)indexPath.item, (long)indexPath.section);
            [indexPaths addObject:indexPath];
        }
        [weakSelf.collectionView performBatchUpdates:^{
            [weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
        } completion:^(BOOL finished) {

        }];

//            [weakSelf.collectionView reloadData];
    }];
}

当我运行时,我可以去我的ViewController并看到加载的资产的第一页。 如果向下滚动,我会看到页脚视图(其中包含微调框),但是代码将在异常中断点处断开:

[weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];

- [UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:],/SourceCache/UIKit/UIKit-3185.20/UICollectionViewData.m:829中的断言失败


然后,如果我继续它与错误崩溃:

2014-06-11 16:39:58.335 Radius-iOS [4901:525006] *由于未捕获异常'NSInternalInconsistencyException',原因:'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionFooter at path {length = 2,path = 0 - 0}'*第一次抛出调用堆栈:


这令我感到困惑。 layoutAttributesForSupplementaryElementOfKind听起来像是与布局类有关的事情,但我没有使用自定义流布局,而是使用默认提供的布局。 这听起来像苹果的代码是疯了,但我觉得我正确使用一切。

现在,如果我移动电话:

[self loadNextPageOfAssets];

从补充单元出列到UICollectionViewCell deque,并且一起删除页脚视图,然后插入工作很好。

现在我打电话reloadData而不是插入,但这是丑陋的。

我是否忽略了关于页脚的一些内容?


I've got a typical UICollectionView which is using UICollectionViewFlowLayout in a vertical fashion. I'm using a rest API with pagination to populate the collection view. In order to trigger the next page to download, I'm using the delegate when it asks for the footer layout:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    RDLoadMoreReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"RDLoadMoreReusableView" forIndexPath:indexPath];
    view.delegate = self;
    [view start];
    [self loadNextPageOfAssets];
    return view;
}

Here is the code behind my loadNextPageOfAssets:

-(void)loadNextPageOfAssets{
    __weak RDRadiusGridViewController *weakSelf = self;

    // Increment page and request assets. They are added to cluster.assets before the completion block is called.
    self.cluster.pagination.page++;
    [self.cluster requestAssetsWithCompletionBlock:^(NSArray *assets) {

        SM_LOG_DEBUG(@"page.total: %ld assets.count: %ld", self.cluster.pagination.totalCount, (long)assets.count);

        NSMutableArray *indexPaths = [[NSMutableArray alloc]initWithCapacity:assets.count];
        for(NSUInteger index = 0; index < assets.count; index++){
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.cluster.assets.count - assets.count + index inSection:0];
            SM_LOG_DEBUG(@"Inserting indexPath: %ld:%ld", (long)indexPath.item, (long)indexPath.section);
            [indexPaths addObject:indexPath];
        }
        [weakSelf.collectionView performBatchUpdates:^{
            [weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
        } completion:^(BOOL finished) {

        }];

//            [weakSelf.collectionView reloadData];
    }];
}

When I run I can go to my ViewController and see the first page of assets loaded. If I scroll down I'll see the footer view (which contains a spinner), but then the code will break at the exception break point at line:

[weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];

Assertion failure in -[UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:], /SourceCache/UIKit/UIKit-3185.20/UICollectionViewData.m:829


Then if I continue it crashes with the error:

2014-06-11 16:39:58.335 Radius-iOS[4901:525006] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionFooter at path {length = 2, path = 0 - 0}' * First throw call stack:


This is puzzling to me. layoutAttributesForSupplementaryElementOfKind sounds like it's seomthing to do with the layout class, but I'm not using a custom flow layout rather the default one supplied. It sounds like Apple's code is mad but I feel that I'm using everything correctly.

Now if I move the call:

[self loadNextPageOfAssets];

from the supplementary cell dequeue to teh UICollectionViewCell deque, and remove the footer views all together, then the insert works great.

For now I'm calling reloadData instead of insert, but this is UGLY.

Am I overlooking something about the footers?


原文:https://stackoverflow.com/questions/24174456
更新时间:2022-09-18 19:09

最满意答案

我知道了。 我不得不向自定义类添加更多代码:

头文件:

//
//  CustomCCNode.h
//  Coco2dTest2
//
//  Created by Ethan Mick on 3/11/10.
//  Copyright 2010 Wayfarer. All rights reserved.
//

#import "cocos2d.h"


@interface CustomCCNode : CCSprite <CCTargetedTouchDelegate> {

}

@property (nonatomic, readonly) CGRect rect;


@end

执行:

//
//  CustomCCNode.m
//  Coco2dTest2
//
//  Created by Ethan Mick on 3/11/10.
//  Copyright 2010 Wayfarer. All rights reserved.
//

#import "CustomCCNode.h"
#import "cocos2d.h"


@implementation CustomCCNode

- (CGRect)rect
{
    CGSize s = [self.texture contentSize];
    return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}



- (BOOL)containsTouchLocation:(UITouch *)touch
{
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}

- (void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}


- (void)onExit
{
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super onExit];
}   





- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"ccTouchBegan Called");
    if ( ![self containsTouchLocation:touch] ) return NO;
    NSLog(@"ccTouchBegan returns YES");
    return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

    NSLog(@"ccTouch Moved is called");
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"ccTouchEnded is called");
}


@end

I got it. I had to add some more code to the custom class:

Header File:

//
//  CustomCCNode.h
//  Coco2dTest2
//
//  Created by Ethan Mick on 3/11/10.
//  Copyright 2010 Wayfarer. All rights reserved.
//

#import "cocos2d.h"


@interface CustomCCNode : CCSprite <CCTargetedTouchDelegate> {

}

@property (nonatomic, readonly) CGRect rect;


@end

Implementation:

//
//  CustomCCNode.m
//  Coco2dTest2
//
//  Created by Ethan Mick on 3/11/10.
//  Copyright 2010 Wayfarer. All rights reserved.
//

#import "CustomCCNode.h"
#import "cocos2d.h"


@implementation CustomCCNode

- (CGRect)rect
{
    CGSize s = [self.texture contentSize];
    return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}



- (BOOL)containsTouchLocation:(UITouch *)touch
{
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}

- (void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}


- (void)onExit
{
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super onExit];
}   





- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"ccTouchBegan Called");
    if ( ![self containsTouchLocation:touch] ) return NO;
    NSLog(@"ccTouchBegan returns YES");
    return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

    NSLog(@"ccTouch Moved is called");
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"ccTouchEnded is called");
}


@end

相关问答

更多
  • UITouch *fingerOne = [touchArray objectAtIndex:0]; UITouch *fingerTwo = [touchArray objectAtIndex:1]; 无法保证您在触摸事件中总能收到两次触摸。 首先测试touchArray中使用的触摸次数 UITouch *fingerOne = [touchArray objectAtIndex:0]; UITouch *fingerTwo = nil; if (touchArray.count > 1) { ...
  • 我知道了。 我不得不向自定义类添加更多代码: 头文件: // // CustomCCNode.h // Coco2dTest2 // // Created by Ethan Mick on 3/11/10. // Copyright 2010 Wayfarer. All rights reserved. // #import "cocos2d.h" @interface CustomCCNode : CCSprite { } @prope ...
  • 这些是2d滚动平台游戏的优秀教程 http://www.raywenderlich.com/15230/how-to-make-a-platform-game-like-super-mario-brothers-part-1 http://www.raywenderlich.com/15267/how-to-make-a-platform-game-like-super-mario-brothers-part-2 These are excellent tutorials for 2d scrolling ...
  • 试试这个,没有测试代码,根据你的需要改进它 -(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event { CGSize winSize = [[CCDirector sharedDirector] winSize]; CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; ...
  • 问题是你在触摸开始时创建了操纵杆,并在触摸结束时将其移除。 这不允许操纵杆接收触摸开始事件,因为事件在创建操纵杆时已经被处理。 由于不必要的保留,你也在泄漏操纵杆(请使用ARC!)。 尝试在init中创建操纵杆,禁用并隐藏它,直到收到触摸开始事件。 这也比在每次触摸时重新创建操纵杆更快。 此外,如果您启用了多个触摸,请记住,您最多可以接收5个触摸开始事件(5个手指),两者之间没有任何触摸。 这会产生5个操纵杆,但只能移除一个! 每次! The problem is that you create the j ...
  • 在图层的init方法中,您需要通过设置self.isTouchEnabled = YES;来告诉它响应触摸self.isTouchEnabled = YES; 。 In the init method of the layer, you need to tell it to respond to touches by setting self.isTouchEnabled = YES;.
  • 澄清评论后编辑的答案 :如果您想根据按钮所处的状态(即正常,突出显示,禁用,选择)更改按钮/标签或背景精灵帧的颜色,您可以设置不同的背景/标签属性例如: [button setLabelColor:[CCColor redColor] forState: CCControlStateHighlighted]; 按下按钮时,上面的示例将使按钮标签闪烁红色。 如果您需要更详细地控制应该发生的事情(动画或其他),您将需要创建CCButton或CCControl(它是CCButton继承的基类)的自定义子类,具体取 ...
  • 起亚ora。 无聊迫使我提供关于这个话题的答案。 图层部分(即@interface GetMyTouches:CCLayer): -(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event { UITouch *touchMyMinge = [inappropriateTouches anyObject]; CGPoint currentTouchArea = [touchMyMinge l ...
  • 您需要像这样转换您的触摸位置。 CGPoint location = [touch locationInView: [touch view]]; location = [[CCDirector sharedDirector] convertToGL: location]; 然后,您在cocos2d中的视图上获得正确的触摸位置。 You need to convert Your touch location like this. CGPoint location = [touch locationI ...
  • 确保您启用了触摸功能: -(void)onEnter { [super onEnter]; self.touchEnabled = YES; } 使用boundingBox获取rect。 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *myTouch = [touches anyObject]; CGPoint touchLocation = [myTouch ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)