0

当我运行我的应用程序时,我发现了一个信号 SIGABRT 线程。这是错误消息:[__NSCFConstantString _isResizable]: unrecognized selector sent to instance 0x5c20

问题来自 [[self myCollectionView]setDataSource:self]; 因为当我评论它时它消失了。

据我了解, myCollectionView 数据源的类型和 self 不一样。这就是为什么我有我的错误。

谢谢你的帮助

皮埃尔

CalViewController.h

#import <UIKit/UIKit.h>

@interface CalViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@end

CalViewController.m

#import "CalViewController.h"

#import "CustomCell.h" 

@interface CalViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
}

@end

@implementation CalViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[[self myCollectionView]setDataSource:self];
[[self myCollectionView]setDelegate:self];

arrayOfImages = [[NSArray alloc]initWithObjects:@"chibre.jpg",nil];
arrayOfDescriptions =[[NSArray alloc]initWithObjects:@"Test",nil];
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ 
return [arrayOfDescriptions count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *cellIdentifier=@"Cell";
    CustomCell *cell =  ([collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]);

    [[cell myImage]setImage:[arrayOfImages objectAtIndex:indexPath.item]];
    [[cell myDescriptionLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];

    return cell;
}

- (NSInteger)numberOfSections:(UICollectionView *) collectionView
                                          {return 1;
                                          }


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
4

2 回答 2

6

arrayOfImages是一组图像名称(字符串),因此setImage无法使用。代替:

[[cell myImage]setImage:[arrayOfImages objectAtIndex:indexPath.item]];

您可能打算:

[[cell myImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];

或者,等效地:

cell.myImage.image = [UIImage imageNamed:arrayOfImages[indexPath.item]];

您甚至可能想要重命名arrayOfImagesarrayOfImageNames(或只是imageNames或其他)以消除这种可能的混淆来源。

(顺便说一句,最好不要将实际图像放入数组中。我们应该始终cellForItemAtIndexPath根据数组中的图像名称创建图像对象。)

于 2013-07-17T17:35:58.083 回答
0

尝试替换[[cell myImage]setImage:[arrayOfImages objectAtIndex:indexPath.item]];

NSString *imageName = [arrayOfImages objectAtIndex:indexPath.item];
[cell.myImage setImage:[UIImage imageNamed:imageName];

问题是现在您正试图将 a 分配给NSString应该是 a 的东西UIImage

于 2013-07-17T17:16:33.650 回答