Reputation: 553
I want to populate a data in uitableview. I have a different sections with different category and each category contains different number of rows.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return allCategory.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *heading = [allCategory objectAtIndex:section];
return heading;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int count = 0;
NSString *heading = [allCategory objectAtIndex:section];
for (Product * cat in productArry) {
if ([cat.category containsString:heading]) {
count ++;
}
}
return count;
}
This is a code in which I return a number of rows on a basis of category. For example I have a category name "Engine" which return one number of row and category name "Blocks" which return 2 number of rows. Now I want to populate a data against each category in "cellForRowAtIndexPath "method. How can I populate a correct data against each category?
Upvotes: 2
Views: 204
Reputation: 2557
Option 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;
}
Option 2: Or you may want to make a filtered array
.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;
}
Input
<__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>
)
Output
<__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>
)
)
So
- (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;
}
Upvotes: 1
Reputation: 2423
First you should filter out array by categories..Like below...
NSMutableArray *arrayCategoryProductArray = [[NSMutableArray alloc]init];
for(int i =0 ;i< allCategory.count;i++){
NSString *strCat = [allCategory objectAtIndex:0];
NSArray *filteredarray = [productArry filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(category == %@)", strCat]];
[arrayCategoryProductArray addObject : filteredarray];
}
[YOUR_TBL reloadData];
Apply filter according to your requirement and check filtered result... And in table method....
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *subDataArray = [arrayCategoryProductArray objectAtIndex:section];
return subDataArray.count;
}
Upvotes: 1