【问题标题】:Filter Array results before showing in Table View在表格视图中显示之前过滤阵列结果
【发布时间】:2014-02-21 01:29:29
【问题描述】:

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

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

(我正在使用 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;
    }

【问题讨论】:

    标签: ios uitableview nsarray restkit


    【解决方案1】:

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

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

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

    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

    【讨论】:

    • 如果我做后者,它会在tableView cellForRowAtIndexPath 内的Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row]; 行上崩溃,并在控制台中显示错误[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'...关于原因的任何想法?感谢您的帮助!
    • 如果我做前者,那段代码会出现在我的viewDidLoad 末尾吗?
    • 不,该代码应该在您收到响应时在 [loadObjectsAtResourcePath:usingBlock:] 内,然后再重新加载数据。
    • 另外,对我崩溃的后一种解决方案有任何想法吗?
    • 哦,哇,我犯了一个(大)错误,尝试使用 MIN(x,y) 而不是 MAX。生病编辑答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2011-09-28
    相关资源
    最近更新 更多