Ios 向UICollectionViewCell添加图像时应用程序崩溃
我需要在UICollectionViewCell上添加图像和文本。 所以我用了这个代码Ios 向UICollectionViewCell添加图像时应用程序崩溃,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,我需要在UICollectionViewCell上添加图像和文本。 所以我用了这个代码 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cvCell"; CVCell *cell = (CVCell *)[colle
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cvCell";
CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSString *cellData = [firstSection objectAtIndex:indexPath.row];
[cell.titleLabel setText:cellData];
NSString *imageName = [second objectAtIndex:indexPath.row];
NSLog(@"CV setting image for row %ld from file in bundle with name '%@'", (long)indexPath.row, imageName);
cell.myImageView.image = [UIImage imageNamed:imageName];
return cell;
}
第一节数组有一些名称,第二节数组有一些图像。显示文本,但图像不起作用
所以请告诉我我的代码有什么问题
我犯了这样的错误:
2014-11-07 15:18:56.928 CollectionViewExample[4459:151457] CV setting image for row 0 from file in bundle with name '(
"real_estate_2.png",
"photography_2.png",
"auto_2.png",
"electronics_2.png",
"services_2.png"
)'
2014-11-07 15:18:56.929 CollectionViewExample[4459:151457] -[__NSArrayI length]: unrecognized selector sent to instance 0x7be86240
2014-11-07 15:18:57.044 CollectionViewExample[4459:151457] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7be86240'
*** First throw call stack:
(
0 CoreFoundation 0x02bf5df6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x01975a97 objc_exception_throw + 44
2 CoreFoundation 0x02bfda75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x02b469c7 ___forwarding___ + 1047
4 CoreFoundation 0x02b4658e _CF_forwarding_prep_0 + 14
5 UIKit 0x008ad0be +[_UIAssetManager createAssetNamed:fromBundle:] + 66
6
我在UICollectionView中经常犯的最常见的错误是因为没有写作 [self.myCollectionView注册表类:[MyCollectionViewCell类] forCellWithReuseIdentifier:@“MYCELL”] 您是否尝试过在imageView中提供静态图像?如果它能工作,那么我认为数组中的UIImage可能需要类型转换
点击此链接:错误在于您的新的或旧的
collectionViewCell
现在没有名为(imageView
以前)或myImageOnCollectionView
的属性。这是因为您的集合视图不响应选择器。(选择器与方法相同)。
如果在其实现文件(.h)中有一个自定义的collectionCell
(简易方法),请添加以下内容:
@property (nonatomic, strong) UIImageView *theNameYouWant; // The must be the same you implement en cellForRow...
//将此属性与情节提要表达连接。(请确保在故事板中,单元必须是您自己的单元类)
当您连接时,IBOutlet
出现
@property (nonatomic, strong) IBOutlet UIImageView *theNameYouWant;
//如果您有所有的代码,请告诉我。很抱歉,答案很简单,但它看起来像 imageName是NSArray不是NSString
cell.myImageView.image = [UIImage imageNamed:<NSArray>];
cell.myImageView.image=[UIImage ImageName:];
请记录(@“%@”,秒) @PavanAlapati这看起来可能是您的解决方案,因为为
UICollectionViewCell
而不是myCellCollectionViewCell
引发了无法识别的选择器错误。请注意,目前您正在为“cellIdentifier”注册单元格。如果您正在使用故事板,则无需注册单元格collectionview自定义单元格中存在IBOutlet问题。请在数组中检查它,并确保其中的元素是字符串。它们似乎是数组。