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