插入到包含页脚的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
最满意答案
我知道了。 我不得不向自定义类添加更多代码:
头文件:
// // 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]]]; ...
-
Cocos2d - 允许多个类使用触摸(sneakyjoystick)(Cocos2d - Allow touches to be used by multiple classes (sneakyjoystick))[2024-03-02]
问题是你在触摸开始时创建了操纵杆,并在触摸结束时将其移除。 这不允许操纵杆接收触摸开始事件,因为事件在创建操纵杆时已经被处理。 由于不必要的保留,你也在泄漏操纵杆(请使用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 ...
-
Cocos2d CCTouchBegan(Cocos2d CCTouchBegan)[2023-02-03]
确保您启用了触摸功能: -(void)onEnter { [super onEnter]; self.touchEnabled = YES; } 使用boundingBox获取rect。 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *myTouch = [touches anyObject]; CGPoint touchLocation = [myTouch ...