ios - 动态选择变量

标签 ios objective-c

您好,感谢您抽出宝贵的时间。我对 IOS/Objective C 相当陌生。

我在 View Controller 的顶部全局设置了多个变量。

NSMutableArray * A;
NSMutableArray * B;
NSMutableArray * C;

现在,当有人在表格 View 中选择一个单元格时,我想使用该单元格的名称来选择全局变量之一。我找到了一些可以用 View Controller 来完成此操作的东西,但我也需要一些用于杂项变量的东西。我指的是:

id myNewController = [[NSClassFromString(selected) alloc] init];
[[self navigationController] pushViewController:myNewController animated:YES];

所以它会是这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Save text of the selected cell:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stringVariable = cell.textLabel.text;// value=A,B or C

// match string to array and load that array.
??         

}

提前感谢您的任何帮助,如果有人问过这个问题,我很抱歉,但我找不到任何有效的方法,所以作为最后的手段,我寻求帮助:)

最佳答案

您可能最好将数组存储为字典上的键,而不是作为单独的字段。例如:

NSDictionary* dictionary;

dictionary = @{
    @"A": [[NSMutableArray alloc] init],
    @"B": [[NSMutableArray alloc] init],
    @"C": [[NSMutableArray alloc] init]
};

然后,当您选择行时,您可以通过键查找数组:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString* key = cell.textLabel.text;// value=A,B or C

NSMutableArray* array = dictionary[key];
....

关于ios - 动态选择变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16818979/

相关文章:

ios - 段错误: 11 with swift

iphone - NSString - 如何将 LONDON 转换为伦敦

iphone - textFieldDidBeginEditing 和 textFieldShouldBeginEditing 之间的确切区别是什么

ios - 一个项目文件中的更改正在更改另一项目中的源文件

ios - 自定义单元格中的焦点 UITextfield

ios - 使用带有循环的 Alamofire 根据 Tableview 计数发送多个请求

ios - 在uipickerview中显示选中的值

objective-c - 在 tvOS 中呈现 View Controller

ios - 为什么在重新启动 iOS 模拟器时我的 plist 变成二进制并丢失数据

objective-c - 如何使用两个 xib 文件创建一个 Objective-C 类(一个用于 iPhone,另一个用于 iPad)