@property(nonatomic,strong)NSMutableArray * dataArray; //声明数组的属性
- (NSMutableArray *)dataArray{ //数组懒加载
if (!_dataArray) {
_dataArray = [[NSMutableArray alloc] init];
}
return _dataArray;
}
NSArray * data = @[@"1",@"2",@"3",@"4"]; //将数组每三个分成一组
NSInteger counts = 0;
NSInteger section = 0;
for (int i = 0; I<data.count; i++) {
NSString *str = data[i];
if (counts == 3) {
counts =0;
}
if (i % 3 == 0) { // 找下规律 当 i == 0 时, 0 % 3 = 0; 当i==3时,3%3 = 0
section ++;
NSMutableArray *tagArray = [[NSMutableArray alloc] init];
[self.dataArray addObject:tagArray];
}
if (counts <= 3) {
[self.dataArray[section-1] addObject:model];
counts ++;
}
}
此时得到的数组
(lldb) po self.dataArray
<__NSArrayM 0x6000038e4120>(
<__NSArrayM 0x6000038e4fc0>(
<NSString: 0x6000038e60d0>,
<NSString: 0x6000038e6ca0>,
<NSString: 0x6000038e4c30>
)
,
<__NSArrayM 0x6000038e6d60>(
<NSString: 0x6000038e6af0>
)
网友评论