我正在使用objective-c中的collectionView开发一个iOS应用程序,我有一个错误。
下面的代码用于为每个单元格提供数据(在这种情况下为图像)。 我从Model类中的二维数组中选取一个图像名称,然后显示它。
我制作了一个新阵列,通过将数组中的值从“0”更改为“1”来防止图像显示重复,因此下次可以避免显示“1”标记的图像。
我成功地使用NSInteger 0初始设置所有数组,在'ViewDidLaod'方法中使用replaceObjectAtIndex:withObject:方法,但是,当我想使用replaceObjectAtIndex:withObject将数值更改为'1'时,我有一个错误:,我在'ViewDidLaod'方法中使用的方法。
我已经搜索了谷歌和堆栈,但是那些遇到同样问题的人使用了不可变数组,这就是他们遇到问题的原因,但我的代码中有可变数组。请帮帮我。
错误信息如下:
2015-04-13 03:55:25.824 InfanTree[4474:166010] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000002
2015-04-13 03:55:25.846 InfanTree[4474:166010] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000002'
*** First throw call stack:
(
0 CoreFoundation 0x000000010826fa75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107f08bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000108276d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001081ce9dc ___forwarding___ + 988
4 CoreFoundation 0x00000001081ce578 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000108f13f06 +[_UIAssetManager createAssetNamed:fromBundle:] + 67
6 UIKit 0x00000001088d4f60 +[UIImage imageNamed:inBundle:compatibleWithTraitCollection:] + 221
7 InfanTree 0x00000001079c92d0 -[ViewController collectionView:cellForItemAtIndexPath:] + 512
8 UIKit 0x0000000108ed1fab -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 244
9 UIKit 0x0000000108ed36e4 -[UICollectionView _updateVisibleCellsNow:] + 3445
10 UIKit 0x0000000108ed7391 -[UICollectionView layoutSubviews] + 243
11 UIKit 0x000000010891c1c3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
12 QuartzCore 0x000000010cc5ac58 -[CALayer layoutSublayers] + 150
13 QuartzCore 0x000000010cc4f87e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x000000010cc4f6ee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x000000010cbbd36e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
16 QuartzCore 0x000000010cbbe482 _ZN2CA11Transaction6commitEv + 390
17 UIKit 0x00000001088a068d -[UIApplication _reportMainSceneUpdateFinished:] + 44
18 UIKit 0x00000001088a1375 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2684
19 UIKit 0x000000010889fd35 -[UIApplication workspaceDidEndTransaction:] + 179
20 FrontBoardServices 0x000000010bd92243 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
21 CoreFoundation 0x00000001081a4c7c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
22 CoreFoundation 0x000000010819a9c5 __CFRunLoopDoBlocks + 341
23 CoreFoundation 0x000000010819a183 __CFRunLoopRun + 851
24 CoreFoundation 0x0000000108199bc6 CFRunLoopRunSpecific + 470
25 UIKit 0x000000010889f7a2 -[UIApplication _run] + 413
26 UIKit 0x00000001088a2580 UIApplicationMain + 1282
27 InfanTree 0x00000001079d19d3 main + 115
28 libdyld.dylib 0x000000010aa60145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
整个代码为here
以下是我的代码的一部分。
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
DataModel *_data;
// save the indexPath value itself of the picture which is selected at specific index path
NSMutableArray *_displayedImageIndexPathHistory;
// save 1 for the first rendering so next time can avoid to be selected
NSMutableArray *_isSelectedBefore;
// save 1 if rendered at least once so can avoid duplication
NSMutableArray *_preventDuplication;
}
@end
@implementation ViewController
.
.
.
- (void)viewDidLoad {
[super viewDidLoad];
.
.
.
// _data.imageSet is a two dimensional 'MUTABLE' array which has 'MUTABLE' arrays in it.
// Declared in a Model class. Each element has NSString *imageName.png.
_preventDuplication = [_data.imageSet mutableCopy];
_displayedImageIndexPathHistory = [_data.imageSet mutableCopy];
_isSelectedBefore = [_data.imageSet mutableCopy];
// set all arrays with 0
NSInteger sectionNum = [_data.imageSet count];
NSNumber *defaultNumber = [NSNumber numberWithInt:0];
for (NSInteger i = 0; i < sectionNum; i++) {
for (NSInteger j = 0; j < [_data.imageSet[i] count]; j++) {
[_displayedImageIndexPathHistory[i] replaceObjectAtIndex:j withObject:defaultNumber];
[_isSelectedBefore[i] replaceObjectAtIndex:j withObject:defaultNumber];
[_preventDuplication[i] replaceObjectAtIndex:j withObject:defaultNumber];
}
}
.
.
.
}
.
.
.
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSNumber *defaultNumber = [NSNumber numberWithInt:0];
NSNumber *markingNumber = [NSNumber numberWithInt:1];
if (indexPath.row == 0) {
cell.cellImageView.image = [UIImage imageNamed:[_data.imageSet[indexPath.section] objectAtIndex:indexPath.row]];
// **Have error at this sentence!!**
[_preventDuplication[indexPath.section] replaceObjectAtIndex:indexPath.row withObject: markingNumber];
}
else {
.
.
.
}
.
.
return cell;
}
提前致谢。
答案 0 :(得分:2)
您的代码的表达式为:
[UIImage imageNamed:[_data.imageSet[indexPath.section] objectAtIndex:indexPath.row]]
看来这部分:
[_data.imageSet[indexPath.section] objectAtIndex:indexPath.row]
正在生成NSNumber
的实例,而不是NSString
的实例。当然,+[UIImage imageNamed:]
要求其参数为字符串。它显然在其上调用了-length
。由于NSNumber
没有回复-length
,因此您会收到该异常并崩溃。