Pergunta

Im trying to create a class which uses UICollectionView to display a simple menu, but I am running into endless problems, and some of the most cryptic error messages Ive ever seen.

Here is the code, which was working fine last week:

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

Cryptic error message:

Thread 1: signal SIGABRT

Which appears in main.m, on the line that returns UIApplicationMain.

Trace Output:

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) 

From this trace output, I am guessing one of the arguments sent to collectionView:numberOfItemsInSection is of the wrong type, but I have no idea where I would fix that problem, because as far as I can tell this would be a problem with the framework itself, and not the code Ive written.

Am I completely wrong? Can anyone shed any light on this?

After reading online about the SIGABRT error, I have tried resetting the simulator and rebooted Xcode and the MAc itself several times, but to no avail.

Any help would be hugely appreciated.

Foi útil?

Solução

How are you initialising this Menu class?

Looks to me like the Menu class is being released too early. Make sure whatever property you are calling [[Menu alloc] initWithClockArray: ] on has the strong attribute.

Outras dicas

Start by adding the call self = [super init]; to the top of initWithClockArray.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top