objective-c - 如何在 Cocoa 中将过滤器应用于 NSArrayController 后从 NSArray 获取值

标签 objective-c cocoa cocoa-bindings nsarraycontroller

我正在学习 cocoa 中的绑定(bind)。我的 ArrayController 绑定(bind)到 TableView 和搜索字段。数据显示和过滤都很好。 这些属性位于 mutableArray 中。

@property (nonatomic, strong) NSString *displayName;
@property NSInteger entityID;
@property (nonatomic, strong) NSString *MemberStatus;

在表中,我显示“displayName”。我的主要问题是,我需要选择一行并使用所选成员的“entityID”运行 Web 服务。

如果我不过滤并选择一行,我可以使用选定的行索引从可变数组中获取所需的“entityID”。

[[[self.participantArray objectAtIndex:selectedRow] valueForKey:@"entityID"] integerValue] 

每当我进行过滤时,根据所选的行索引,它自然会返回错误的值。如何获取正确的字段?请帮忙。如果我的问题不清楚,请提及您的需求。 谢谢。

最佳答案

表格 View 显示数组 Controller 的arrangedObjects。因此,您也应该对此进行索引。

您将需要一个阵列 Controller 的 socket ,以便您可以执行以下操作:

[[[self.particpantsController.arrangedObjects objectAtIndex:selectedRow] valueForKey:@"entityID"] integerValue]

(我实际上不确定为什么在这里使用 -valueForKey: 。表达式 [self.particpantsController.arrangedObjects objectAtIndex:selectedRow] 为您提供对象。如果它有一个 entityID 属性,你可以只调用该对象的 getter。如果它是一个 NSDictionary,那么你应该更喜欢 -objectForKey: 优于 -valueForKey:。事实上,您应该更喜欢创建一个真实的模型对象类并使用它,而不是 NSDictionary。制作字典往往在玩具应用程序中效果很好,原型(prototype)设计,但很快就显示出其局限性。)

关于objective-c - 如何在 Cocoa 中将过滤器应用于 NSArrayController 后从 NSArray 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341206/

相关文章:

swift - 在 Swift 类中更改绑定(bind)变量

objective-c - 绑定(bind)到 NSDictionary 的 "allValues"数组

ios - 我需要一个可变数组,它在 IOS 中有 8 个内插字符串

ios - Segue 不会传递数据

objective-c - 枚举包含字典的数组会产生意外的输出

ios - 函数可以从异步 block 返回值吗

php - 从 PHP 中的 RNCryptor AES 256 header 中检索 IV

macos - 将 NSMutableArray 绑定(bind)到 NSPopUpButton 并插入新值

iphone - 在运行时检查 iOS 版本?

objective-c - 在 Cocoa 应用程序中使用什么更好 : dynamic libraries or static libraries?