今天碰到一個需求,如下圖扔涧,接口返回的是一個列表园担,需要移動端每三個做一頁届谈,
這就需要對列表進行處理,每三個放一堆弯汰,重新放到一個數組里艰山,然后cell里放個tableview,用新數據賦值即可咏闪。
/*
處理前:
@[@"", @"", @"", @"", @"", @"", @"", @"", @""];
處理后:
@[@[@"", @"", @""], @[@"", @"", @""], @[@"", @"", @""]];
*/
//** 創(chuàng)建處理前的數據
NSMutableArray *oldList = [NSMutableArray array];
for (int i = 0; i < 20; i++) {
[oldList addObject:[NSString stringWithFormat:@"hh-%d", i]];
}
NSLog(@"原始數據 %@", oldList);
// 處理數據
NSMutableArray *newList = [NSMutableArray array];
NSMutableArray *tep = [NSMutableArray array];
for (int i = 0; i < oldList.count; i++) {
[tep addObject:oldList[i]];
// 如果遍歷完了曙搬,最后一次的可能不滿也送走
if (i == oldList.count-1) {
[newList addObject:tep.mutableCopy];
[tep removeAllObjects];
}
// 加滿三個就送走
if (tep.count == 3) {
[newList addObject:tep.mutableCopy];
[tep removeAllObjects];
}
}
NSLog(@"處理后的數據 %@", newList);