我正在尝试通过从Storyboard中调用它来将uicollectionview作为子视图添加到名为CollectionSpace的View中。
UIViewController *CollectionVC =[self.storyboard instantiateViewControllerWithIdentifier:@"CollectionVC"];
CollectionVC.view.frame = CGRectMake(CollectionSpace.frame.origin.x-276, CollectionSpace.frame.origin.y-87, CollectionSpace.frame.size.width+2, CollectionSpace.frame.size.height-2);
[CollectionSpace addSubview:CollectionVC.view];
故事板有一个集合视图,其中委托和源,作为uicollectionview已链接到所有者视图控制器。这是代码:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) IBOutlet UICollectionView *collectionView; //linked
@end
@implementation ViewController
- (void)viewDidLoad
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(150, 150);
}
所以这很好用,它加载了uicollectionview,我可以看到细胞。滚动时出现问题。滚动后,应用程序崩溃了。 (lldb)
我尝试通过导入视图控制器以不同的方式调用它并启动它:
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setItemSize:CGSizeMake(200, 140)];
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
ViewController *CollectionVC = [[ViewController alloc]initWithCollectionViewLayout:aFlowLayout];
但是uicollectionview没有显示出来。 知道什么可能导致崩溃吗?