1

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

这是上周运行良好的代码:

菜单.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

菜单.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 本身几次,但无济于事。

任何帮助将不胜感激。

4

2 回答 2

1

你是如何初始化这个菜单类的?

在我看来,菜单类发布得太早了。确保您调用[[Menu alloc] initWithClockArray: ]的任何属性都具有该strong属性。

于 2013-07-29T10:59:04.013 回答
0

首先将调用添加self = [super init];initWithClockArray.

于 2013-07-29T09:56:39.097 回答