• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用部分类别填充表格 View

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:12:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
        return allCategory.count;
}

- (NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section {
    NSString *heading = [allCategory objectAtIndex:section];
    return heading;
}

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section{
    int count = 0;
     NSString *heading = [allCategory objectAtIndex:section];
    for (Product * cat  in productArry) {
        if ([cat.category containsString:heading]) {

            count ++;
        }
    }

    return count;

}

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



Best Answer-推荐答案


选项 1:

- (Product *)dataForRowAtIndexPathNSIndexPath *)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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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>(
<roduct: 0x78668460; category = A; name = A1>,
<roduct: 0x78663280; category = B; name = B1>,
<roduct: 0x7866c750; category = A; name = A2>,
<roduct: 0x7864c950; category = B; name = B2>,
<roduct: 0x786610a0; category = B; name = B3>
)

输出

<__NSArrayM 0x798449b0>(
<__NSArrayI 0x79839050>(
<roduct: 0x78668460; category = A; name = A1>,
<roduct: 0x7866c750; category = A; name = A2>
)
,
<__NSArrayI 0x798c7310>(
<roduct: 0x78663280; category = B; name = B1>,
<roduct: 0x7864c950; category = B; name = B2>,
<roduct: 0x786610a0; category = B; name = B3>
)
)

所以

- (Product *)dataForRowAtIndexPathNSIndexPath *)indexPath {
    Product *product = [[self.products objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return product;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Product *product = [self dataForRowAtIndexPath:indexPath];
    return nil;
}

关于ios - 使用部分类别填充表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329645/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap