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

ios - 在表格 View 中显示之前过滤阵列结果

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

我需要过滤我正在使用的数组,这样它就不会在我的 UITableView 中显示所有结果(只想显示 100 个 leafs 中的前 20 个) .

不知道该怎么做。如果您想发布更多代码,请告诉我!

(我正在使用 RestKit,从 API 中提取,并且对象映射已经正常工作)

ViewController.m

    @property (strong, nonatomic) NSArray *springs;
    @property (strong, nonatomic) NSMutableArray *leafs;

    - (void)viewDidLoad
    {
        [super viewDidLoad];
       [[RKObjectManager sharedManager] 
       loadObjectsAtResourcePath"/ss/?apikey=xx" 
       usingBlock:^(RKObjectLoader *loader) {
            loader.onDidLoadObjects = ^(NSArray *objects){

                  springs = objects;

// @cream-corn this is the for statement you suggested, but I can't finish it
        for (Sport *sport in objects){
                for (Leaf *leaf in spring.leafs){
                    if (leaf.abbreviation isEqualToString"ap"){
// This is where I can't figure out what to put
                        [];
                    }
                }
            }


              [_tableView reloadData];


          // @cream-corn this is where I log that I'm getting correct data
            for (Spring *sppring in objects){
             NSLog(@"%@", spring.name);
            for (Leaf *leaf in spring.leafs){
              NSLog(@"     %@ %@", leaf.name, leaf.abbreviation);
                 }
            }


            };

            [loader.mappingProvider  
        setMapping:[Spring mapping] 
        forKeyPath"springs"];
            loader.onDidLoadResponse = ^(RKResponse *response){

            };
        }];
    }

    - (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
    {
        Spring *spring = [springs objectAtIndex:section];
        return spring.leafs.count;
    }

    - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"standardCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        Spring *spring = [springs objectAtIndex:indexPath.section];  // 13 objects
        Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row];  // 30 objects

        cell.textLabel.text = leaf.shortName;
        return cell;
    }



Best Answer-推荐答案


好的,所以。你想专门过滤数组吗?即:删除满足特定条件的对象? 或者只在表格 View 中显示前 20 个?

如果前者是正确的(假设这个数组是可变的),你可以这样做:(这是一种伪代码,此代码不会复制/粘贴)

for(id obj in [myMutableArray reverseObjectEnumerator]) {
    if(obj does not meet condition) {

        [myMutableArray removebj]

    }
}

reverseObjectEnumerator 是这个循环中最重要的部分,没有它;它会抛出异常,因为您在枚举时发生了变异。

如果后者是正确的,你可以这样做:

   - (NSInteger)tableViewUITableView *)tableView numberOfRowsInSection (NSInteger)section
{
    Spring *spring = [springs objectAtIndex:section];
    return MIN(spring.leafs.count, 20);
}

return MIN(spring.leafs.count,20); 行只返回较小的数字,spring.leafs.count20

关于ios - 在表格 View 中显示之前过滤阵列结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923401/

回复

使用道具 举报

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

本版积分规则

关注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