繁体   English   中英

ios中的二维数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM