【问题标题】:UICollectionView - Please help- getting a "type NSException" error when runningUICollectionView - 请帮助 - 运行时出现“类型 NSException”错误
【发布时间】: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{ }

标签: xcode uicollectionview


【解决方案1】:

您好,请务必注册您的单元格 xib 文件。

弹出这个:

[self.collectionView registerNib:[UINib nibWithNibName:@"YourXibCellNameHere" bundle:nil] forCellWithReuseIdentifier:@"PatternCell"];

在collectionViewController 类中的“ViewDidLoad”方法中。应用程序需要知道用于填充 UIContentView 的 xib 文件。

如果您的单元格没有使用 Xib 文件,只需使用自定义单元格视图控制器的名称即可。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多