【问题标题】:Remove selected API results before displaying in Table View在表格视图中显示之前删除选定的 API 结果
【发布时间】:2013-05-22 23:25:06
【问题描述】:

我正在使用 RestKit 处理来自 API 的数据。

我想获得一份企业列表,并且正在正确地获得它。然而,一旦我获得了该列表,我需要删除所有属于“麦当劳”的企业在我在UITableView 中显示结果之前

示例响应:

"venue": [{
    "name": "X Business", {

没有办法在 API 响应之前删除这些结果,有没有办法在我得到 API 响应但在UITableView 中显示结果之前执行此操作?

编辑:

视图控制器

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


    - (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;
    }

编辑 2: Response.m 是模型

@implementation Response

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"premium", @"premium",
         nil];
    }];

    return objectMapping;
}

@end

【问题讨论】:

  • 您始终可以控制何时要在表格视图中显示结果。例如,您可以在调用表视图重新加载数据之前过滤您的数据。
  • @verbumdei 感谢您的回复!是的,我知道这是可能的,但我就是不知道该怎么做。我是否从响应中创建另一个字典,或者最好的方法是什么?
  • 目前如何设置表格视图的数据源?你在使用 NSArray 吗?您能否发布一些有关如何设置此数据源属性的代码?
  • @verbumdei 当然,我在编辑中添加了一些代码。我正在使用 RestKit 映射。

标签: xcode json uitableview restkit


【解决方案1】:

RestKit 可以使用 KVC 对传入的数据进行验证,因此您可以在映射过程中过滤传入的数据(您将在映射过程中节省时间和精力,并且不必对结果进行后期处理)。

查看文档here

【讨论】:

  • 感谢您指点我的文档,我会检查一下 KVC 是否对我有帮助!
  • 我是将这个 KVC 代码放在我为 SpringLeaf 构建的模型类中,还是放在我的视图控制器的 viewDidLoad 中?
  • 验证方法必须在模型类中。
  • 我更新了我的问题以显示模型类,它会在 + (RKObjectMapping *)mapping 已经完成之后作为单独的一组行吗?
  • 是的,这是响应模型类中的一个新方法。该文档显示了方法和示例实现。
【解决方案2】:

您可能只想使用 NSPredicate 过滤从 API 获得的数组,例如,如果每个对象,例如在您的情况下,Spring 在数组内部具有 businessName 属性:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"businessName = %@", @"McDonalds"];
NSArray *filteredArray = [self.springs filteredArrayUsingPredicate:predicate];

【讨论】:

  • 然后只使用*filteredArray 而不是我上面使用的spring.leafs
  • @Reez 是的,如果你需要的话 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多