SIGABRT错误使用UICollectionView时

时间:2013-07-29 09:40:22

标签: objective-c sigabrt

我试图创建一个使用UICollectionView显示简单菜单的类,但我遇到了无数问题,以及我见过的一些最神秘的错误信息。

这是代码,上周工作正常:

Menu.h:

#import <UIKit/UIKit.h>

@interface Menu : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView * menuView;
@property (nonatomic, strong) UIView * containerView;
@property (nonatomic, strong) NSArray * clocks;

- (NGCMenu*) initWithClockArray: (NSArray*) clockArray;
- (void) displayMenuInView: (UIView*) view;
- (void) clearMenu;

@end

Menu.m:

#import "Menu.h"

@implementation Menu
@synthesize menuView,containerView,clocks;

- (Menu*) initWithClockArray: (NSArray*) clockArray {
    clocks=[[NSArray alloc] initWithArray: clockArray];
    return self;
}

- (void) displayMenuInView: (UIView*) view {
    CGRect menuFrame=CGRectMake(50, 50, 200, 500);
    containerView=[[UIView alloc] initWithFrame: menuFrame];
    UIScrollView * scrollView=[[UIScrollView alloc] initWithFrame:containerView.frame];
    UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc] init];
    menuView=[[UICollectionView alloc] initWithFrame:menuFrame collectionViewLayout:layout];
    [menuView setDataSource:self];
    [menuView setDelegate:self];
    [menuView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [scrollView addSubview:menuView];
    [containerView addSubview:scrollView];
    [view addSubview:containerView];
}

- (void) clearMenu {
    [containerView removeFromSuperview];
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger i=(NSInteger) 30;
    return i;
}

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    NSString * labelText=[NSString stringWithFormat:@"%d",indexPath.row];
    UILabel * label=[[UILabel alloc] initWithFrame:cell.frame];
    [label setText:labelText];
    [cell addSubview:label];
    return cell;
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(50, 50);
}

@end

隐秘错误消息:

Thread 1: signal SIGABRT

出现在main.m中,返回UIApplicationMain的行。

跟踪输出:

2013-07-29 10:32:44.016 tyrionCollection2[2620:11303] -[__NSCFDictionary collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x886fed0
2013-07-29 10:32:44.018 tyrionCollection2[2620:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x886fed0'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x543b6f 0x543c0a 0x5454c4 0x51d7d4 0x53338a 0x533fa1 0x5303aa 0x543c9a 0x5442db 0x51bb33 0x652dd 0x10e46b0 0x228ffc0 0x228433c 0x228feaf 0x1042bd 0x4cb56 0x4b66f 0x4b589 0x4a7e4 0x4a61e 0x4b3d9 0x4e2d2 0xf899c 0x45574 0x4576f 0x45905 0x4e917 0x1296c 0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1317a 0x14ffc 0x1c5d 0x1b85)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

从这个跟踪输出中,我猜测发送给collectionView:numberOfItemsInSection的一个参数是错误的类型,但我不知道我在哪里解决这个问题,因为据我所知,这将是框架本身存在问题,而不是我编写的代码。

我完全错了吗?任何人都可以对此有所了解吗?

在线阅读SIGABRT错误后,我尝试重置模拟器并多次重启Xcode和MAc本身,但无济于事。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您是如何初始化此菜单类的?

在我看来,Menu类太早发布了。确保您呼叫[[Menu alloc] initWithClockArray: ]的所有属性都具有strong属性。

答案 1 :(得分:0)

首先将通话self = [super init];添加到initWithClockArray的顶部。