我正在尝试获取单击索引值的行。
我像这样用下一个 Controller 将其插入
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
nextView.count = [counts objectAtIndex:indexPath.row];
counts 正确显示了一个具有 4 个整数值的数组,并且 row 显示了正确的位置,但由于某种原因 nextView.count 变成了一些奇怪的 8 位数字,不知道为什么?
我在 nextViewController 中调用 count ,如下所示:
@interface nextViewController : UIViewController {
int count;
}
@property (nonatomic, assign) int count;
因此,即计数看起来像这样 [3, 6, 2, 5],根据索引,行可能看起来像 0、1、2 或 3,但是 nextView.count 的结果 = 3943493
最佳答案
Objective-C 集合,例如 NSArray
只能存储 Objective-C 对象。
所以
[counts objectAtIndex:indexPath.row]
返回一个对象,可能是
NSNumber
, 但是 nextView.count
是一个(标量)整数。你必须使用
nextView.count = [[counts objectAtIndex:indexPath.row] intValue];
(在您的情况下发生的情况是对象的地址被分配给整数。)
关于ios - IndexPath 没有为 iOS 显示正确的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443392/