【发布时间】:2014-02-11 22:34:12
【问题描述】:
我对 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
_
【问题讨论】:
-
你需要在viewdidload中添加这个方法 - (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier{ }