簡體   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