ios - IndexPath 没有为 iOS 显示正确的数字?

标签 ios iphone objective-c

我正在尝试获取单击索引值的行。

我像这样用下一个 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/

相关文章:

ios - 仍与 Google 保持联系

iphone - YouTube api v3 对 iOS 视频的评论

iphone - Audio Session 属性监听器在 UIImagePickerController 中自动停用?

objective-c - 在 NSTableView 的自定义 NSCell 中使用绑定(bind)公开模型对象

php - iOS 安全性将带有密码的数据发送至服务器或从服务器发送数据

ios - SpriteKit 自定义 UIScrollView 加速

ios - 如何回到 WKWebView 历史记录中的起点

ios - 如何从 crashlytics 禁用崩溃报告弹出窗口

iphone - NSIncation 将 C 数组传递给 Objective-C 方法

iphone - Iphone应用程序的GData示例