【问题标题】:CollectionView [__NSCFConstantString _isResizable]: unrecognized selector sent to instanceCollectionView [__NSCFConstantString _isResizable]:发送到实例的无法识别的选择器
【发布时间】:2013-07-16 08:58:35
【问题描述】:

我在运行我的应用程序时发现了一个信号 SIGABRT 线程。这是错误消息: [__NSCFConstantString _isResizable]:无法识别的选择器发送到实例 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

【问题讨论】:

  • 谢谢罗伯。现在可以使用了!

标签: ios string multithreading sigabrt


【解决方案1】:

您的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]];

您甚至可能希望将 arrayOfImages 重命名为 arrayOfImageNames(或只是 imageNames 或其他名称)以消除这种可能的混淆来源。

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 2019-08-28
      • 2012-07-24
      相关资源
      最近更新 更多