我是Xcode的新手。这是我第一次尝试UICollectionView。任何帮助是极大的赞赏。
这是我得到的错误。
libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)
这是我的代码。
PatternViewCell.h
#import <UIKit/UIKit.h>
@interface PatternViewCell : UICollectionViewCell
@property ( nonatomic, weak)IBOutlet UIImageView *patternImageView;
@property ( nonatomic, weak)IBOutlet UILabel *patternLabel;
@end
#import <UIKit/UIKit.h>
#import "PatternViewCell.h"
@interface collectionViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@end
#import "collectionViewController.h"
@interface collectionViewController ()
@property ( nonatomic, strong) NSArray * patternImagesArray;
@end
@implementation collectionViewController
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PatternViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PatternCell" forIndexPath:indexPath];
NSString * myPatternString = [self.patternImagesArray objectAtIndex:indexPath.row];
cell.patternImageView.image = [UIImage imageNamed:myPatternString];
cell.patternLabel.text = myPatternString;
return cell;
}
-(CGSize) collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(150, 150);
}
-(UIEdgeInsets) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.patternImagesArray = @[@"1.png",@"2.png"@"3.png"@"4.png"];
}
- (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 self.patternImagesArray.count;
}
@end
_
答案 0 :(得分:0)
您好确保注册您的cell xib文件。
弹出这个:
[self.collectionView registerNib:[UINib nibWithNibName:@"YourXibCellNameHere" bundle:nil] forCellWithReuseIdentifier:@"PatternCell"];
进入collectionViewController类的“ViewDidLoad”方法。应用程序需要知道xib文件以使用。
填充UIContentView如果您没有为单元格使用Xib文件,只需使用自定义单元格视图控制器的名称。
祝你好运!