使用节类别填充tableview

时间:2016-03-31 09:38:51

标签: ios objective-c uitableview

我想在uitableview中填充数据。我有一个不同类别的不同部分,每个类别包含不同的行数。

- (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;

}

这是一个代码,我根据类别返回多行。 例如,我有一个类别名称" Engine"返回一行行和类别名称"块"返回2行数。现在我想在" cellForRowAtIndexPath"方法中针对每个类别填充数据。如何针对每个类别填充正确的数据?

2 个答案:

答案 0 :(得分:1)

首先你应该按类别筛选出数组。就像下面......

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];

根据您的要求应用过滤器并检查过滤结果... 在表格方法....

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

     NSArray *subDataArray = [arrayCategoryProductArray objectAtIndex:section];
     return subDataArray.count;

}

答案 1 :(得分:1)

选项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;
}