我有一个实现 UICollectionView 的视图控制器(它是委托、数据源和流布局委托)。我在下一个滚动视图上显示它:
UIViewController *playlistController = [[TNSPlaylistsController alloc] initWithNibName:@"TNSPlaylistsController" bundle:nil];
[self.scrollView addSubview:playlistController.view];
TNSPlaylistsController.h:
#import <UIKit/UIKit.h>
@interface TNSPlaylistsController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@end
TNSPlaylistsController.m:
#import "TNSPlaylistsController.h"
@interface TNSPlaylistsController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> // tried removing these on the m too
@end
@implementation TNSPlaylistsController
@synthesize musicStructure = _musicStructure, collectionView = _collectionView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [self.musicStructure count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellData = [self.musicStructure objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"cvCell";
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
[titleLabel setText:cellData];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.musicStructure = [[NSArray alloc] initWithObjects:@"aaa",@"aaa",@"aaa",@"aaa",@"aaa",@"aaa", nil];
UINib *cellNib = [UINib nibWithNibName:@"TNSPlaylistCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 100)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collectionView setCollectionViewLayout:flowLayout];
NSLog(@"%@\n%@\n%@\n%@\n%@", self, self.musicStructure, self.collectionView, flowLayout, cellNib);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
XIB 包含连接到实现文件的 UICollectionView。
问题是它在启动应用程序时独立地给出 EXC_BAD_ACCESS 或 NSInvalidArgumentException (视图控制器加载到应用程序的第一个视图上)。如果我将视图控制器呈现为普通的模态视图,它可以正常工作
NSInvalidArgumentException跟踪:
2012-10-27 15:32:47.144 Tunes[19255:c07] -[NSConcreteMapTable collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x76524c0
2012-10-27 15:32:47.146 Tunes[19255:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x76524c0'
*** First throw call stack:
(0x1598012 0x12a5e7e 0x16234bd 0x1587bbc 0x158794e 0x71b25f 0x71b2fa 0x71cbb4 0x6f4f14 0x70ab14 0x70b72b 0x707aea 0x71b38a 0x71b9cb 0x6f3273 0x23d92d 0x12b96b0 0x107fc0 0xfc33c 0x107eaf 0x2dc8cd 0x2251a6 0x223cbf 0x223bd9 0x222e34 0x222c6e 0x223a29 0x226922 0x2d0fec 0x21dbc4 0x21ddbf 0x21df55 0x226f67 0x2e75 0x1ea7b7 0x1eada7 0x1ebfab 0x1fd315 0x1fe24b 0x1efcf8 0x25fadf9 0x25faad0 0x150dbf5 0x150d962 0x153ebb6 0x153df44 0x153de1b 0x1eb7da 0x1ed65c 0x2bad 0x2ad5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
EXC_BAD_ACCESS跟踪:
我从来没有遇到过这种错误,所以我完全迷路了。我以几乎相同的方式在其他应用程序上实现了这个 UICollectionView(除了它不在滚动视图上,而是在主视图上)。有什么问题?谢谢!