创建UICollectionView的菜单

时间:2015-01-09 16:29:43

标签: ios objective-c

我正在尝试创建UICollectionView的菜单。

我想按一个单元格并转到一个新的视图控制器。

这是我的代码,没有错误,但应用程序崩溃。

#import "partnerViewController.h"
#import "CVCell.h"

@interface partnerViewController ()

@property (strong,nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation partnerViewController {
    NSArray *menuArray;
}

 (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    menuArray = [NSArray arrayWithObjects:@"HP",@"PureStorage",@"NimbleStorage",@"ExaGrid",@"CommVault",@"Zerto",@"Veeam",@"Coraid",@"Symantec", nil];


    [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [menuArray  count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier;
if (indexPath.row == 0) {
    cellIdentifier = @"0";
} else if(indexPath.row == 1) {
    cellIdentifier = @"1";
} else if(indexPath.row == 1) {
    cellIdentifier = @"2";
} else if(indexPath.row == 1) {
    cellIdentifier = @"3";
} else if(indexPath.row == 1) {
    cellIdentifier = @"4";
} else if(indexPath.row == 1) {
    cellIdentifier = @"5";
} else if(indexPath.row == 1) {
    cellIdentifier = @"6";
} else if(indexPath.row == 1) {
    cellIdentifier = @"7";
} else if(indexPath.row == 1) {
    cellIdentifier = @"8";
}

CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[self.collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"cvCell"];
[cell.titleLabel setText:[menuArray objectAtIndex:indexPath.row]];
[cell.layer setBorderWidth:2.0f];
[cell.layer setBorderColor:[UIColor whiteColor].CGColor];
return cell;
}

@end

这是我得到的错误:

2015-01-09 17:21:15.763 EventAPP[385:22587] -[UICollectionViewCell titleLabel]: unrecognized selector sent to instance 0x13ce16310
2015-01-09 17:21:15.765 EventAPP[385:22587] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell titleLabel]: unrecognized selector sent to instance 0x13ce16310'
*** First throw call stack:
(0x1869c259c 0x1971200e4 0x1869c9664 0x1869c6418 0x1868cab6c 0x10003009c 0x18b1d7bd0 0x18b1d5d30 0x18b1d16d4 0x18b171648 0x18aac9994 0x18aac4564 0x18aac4408 0x18aac3c08 0x18aac398c 0x18aabd3bc 0x18697aa50 0x1869779dc 0x186977dbc 0x1868a50a4 0x18fa475a4 0x18b1da3c0 0x10003231c 0x19778ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你在说:

CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

但你撒谎。此单元格 CVCell。它是一个简单的香草UICollectionViewCell。您是否记得使用此标识符的集合视图注册单元类? (此外,拥有如此多的单元标识符是一件非常奇怪的事情;您是否了解单元标识符的用途?它与重用单元格有关;不要将其用于任何其他目的。)