[英]Two dimensional array in ios
我想在ios中使用二维数组,例如我想为tableview
数据源准备一个数组,
UITableViewCell array[sections][rows];
像这样的东西,在这里我也不能预先确定尺寸。
谢谢
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity: 3];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:0];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:1];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:2];
这就是你从数组中选择值的方法
NSMutableArray *subArray = [dataArray objectAtIndex:2];
NSLog(@"data : %@",[subArray objectAtIndex:0]);
设置NSDictionary
并使用NSIndexPath
作为每个单元格的键
句法糖方法。 请注意,此方法不会自动创建给定大小的NSMutableArray
。 初始数组大小为0,0。
NSMutableArray *yourArray = [@[ [@[] mutableCopy]] mutableCopy];
// add an object to the end of your array
[yourArray[section][row] addObject:@(22)];
// set/change an NSNumber object at a particular location in the 2D array.
yourArray[section][row] = @(22);
// Retrieve a value, converting the NSNumber back to a NSInteger
NSInteger result = [yourArray[section][row] integerValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.