オプション1:
- (Product *)dataForRowAtIndexPath:(NSIndexPath *)indexPath {
int count = 0;
NSString *heading = [self.allCategory objectAtIndex:indexPath.section];
for (Product * product in self.productArry) {
if ([product.category containsString:heading]) {
if (count == indexPath.row) {
return product;
}
count++;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Product *product = [self dataForRowAtIndexPath:indexPath];
return nil;
}
オプション2: それとも、フィルター配列
の.h
@property (nonatomic, strong) NSArray *allCategory;
@property (nonatomic, strong) NSArray *productArry;
@property (nonatomic, strong) NSArray *products;
.M
- (NSArray *)products {
if (!_products) {
NSMutableArray *arrayCategoryProductArray = [[NSMutableArray alloc]init];
for(int i =0 ;i< self.allCategory.count;i++){
NSString *strCat = [self.allCategory objectAtIndex:i];
NSArray *filteredarray = [self.productArry filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(category == %@)", strCat]];
[arrayCategoryProductArray addObject : filteredarray];
}
_products = arrayCategoryProductArray;
}
return _products;
}
入力
<__NSArrayI 0x7866c8b0>(
<Product: 0x78668460; category = A; name = A1>,
<Product: 0x78663280; category = B; name = B1>,
<Product: 0x7866c750; category = A; name = A2>,
<Product: 0x7864c950; category = B; name = B2>,
<Product: 0x786610a0; category = B; name = B3>
)
出力
ようにしたいこと
<__NSArrayM 0x798449b0>(
<__NSArrayI 0x79839050>(
<Product: 0x78668460; category = A; name = A1>,
<Product: 0x7866c750; category = A; name = A2>
)
,
<__NSArrayI 0x798c7310>(
<Product: 0x78663280; category = B; name = B1>,
<Product: 0x7864c950; category = B; name = B2>,
<Product: 0x786610a0; category = B; name = B3>
)
)
ので、アレイ内のカテゴリの
- (Product *)dataForRowAtIndexPath:(NSIndexPath *)indexPath {
Product *product = [[self.products objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return product;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Product *product = [self dataForRowAtIndexPath:indexPath];
return nil;
}
どのように多くの種類? – iOS
推測する配列にX個のカテゴリが存在する可能性があります。 – Devster101
Xカテゴリの量@DarjiJigar – salmancs43