繁体   English   中英

在表视图中显示之前过滤数组结果

[英]Filter Array results before showing in Table View

我需要过滤我正在使用的数组,以便它不会在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)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        Spring *spring = [springs objectAtIndex:section];
        return spring.leafs.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;
    }

可以,然后呢。 您是否要专门过滤数组? 即:删除满足特定条件的对象? 还是仅在表格视图中显示前20个?

如果前者是正确的(假设此数组是可变的) ,则可以执行以下操作:( 这是一种伪代码,此代码不会复制/粘贴)

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

        [myMutableArray remove:obj]

    }
}

reverseObjectEnumerator没有它, reverseObjectEnumerator是该循环中最重要的部分。 它将引发异常,因为您在枚举时正在变异。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM