gpt4 book ai didi

ios - 动态选择变量

转载 作者:行者123 更新时间:2023-11-29 03:53:18 26 4
gpt4 key购买 nike

您好,感谢您抽出宝贵的时间。我对 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com