簡體   English   中英

CollectionView [__NSCFConstantString _isResizable]:無法識別的選擇器已發送到實例

[英]CollectionView [__NSCFConstantString _isResizable]: unrecognized selector sent to instance

運行我的應用程序時,我發現了一個信號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

您的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創建圖像對象。)

嘗試替換[[cell myImage]setImage:[arrayOfImages objectAtIndex:indexPath.item]];

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

問題是,現在您正在嘗試將NSString分配給應該為UIImage

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM