【问题标题】:Filtering NSMutableArray and saving it back in itself过滤 NSMutableArray 并将其保存回自身
【发布时间】:2016-08-19 06:57:06
【问题描述】:

我从nsmutablearray 加载了我的uitableview 数据,但我也想过滤。数据正在完美加载,但现在我需要对其应用过滤功能并重新加载表。到目前为止,这是我为过滤而编写的代码,但它不起作用

NSPredicate *sPredicate;
    for (int i=0; i<TableArray.count; i++) {
        float hotelDistanceFloat = [[[TableArray objectAtIndex:i]xmlhotel_distance] floatValue];
        NSInteger hotelPrice = [[[TableArray objectAtIndex:i]xmlhotel_price] integerValue];

        sPredicate = [NSPredicate predicateWithFormat:@"(%ld <= %d AND %f <= %d)" , (long)hotelPrice, numberOfBudget, hotelDistanceFloat,numberOfDistance];
        TableArray = [[TableArray filteredArrayUsingPredicate:sPredicate] mutableCopy];

    }

numberOfBudgetnumberOfDistance 是从 uislider 获取的简单 int 值。 TableArray 是我的 mutablearray,其中包含所有 tabledata。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return TableArray.count;
}

这些是我的 nsmutablearray 包含的值

NSLog(@"Name : %@",[[TableArray objectAtIndex:1]xmlhotel_name]);
NSLog(@"City : %@",[[TableArray objectAtIndex:1]xmlhotel_city]);
NSLog(@"Price : %@",[TableArray objectAtIndex:1]xmlhotel_price);
NSLog(@"Distance : %@",[TableArray objectAtIndex:1]xmlhotel_distance);
NSLog(@"Image : %@",[[TableArray objectAtIndex:1]xmlhotel_image]);
NSLog(@"Stars : %@",[[TableArray objectAtIndex:1]xmlhotel_stars]);

所有这些值都是字符串

【问题讨论】:

  • 你没有正确预测,数组数据没有caparison?
  • hotelDistanceFloat 和 hotelPrice 是来自数组的数组值!对不起,我没明白,请你再解释一下。
  • 你能显示包含你的数组的值吗?
  • 请检查更新的问题
  • 你的对象 [TableArray objectAtIndex:1] 是 NSDictionary 吗?

标签: ios objective-c iphone uitableview nspredicate


【解决方案1】:

您可能可以删除 for 循环,您不需要每次都分配数组和创建谓词。

   NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"(xmlhotel_price.intValue <= %d AND xmlhotel_distance.floatValue <= %f)", numberOfBudget, numberOfDistance];
   NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[TableArray filteredArrayUsingPredicate:sPredicate]];

【讨论】:

  • 原因:'-[__NSCFNumber 长度]:无法识别的选择器发送到实例 0xb000000000003e82'
  • 酷,属性是字符串,所以转换成intValue/floatValue会修复崩溃:-)
【解决方案2】:

我不确定您所面临的确切问题,但这种和平的代码看起来很可疑:

NSPredicate *sPredicate;
for (int i=0; i<TableArray.count; i++) {
    float hotelDistanceFloat = [[[TableArray objectAtIndex:i]xmlhotel_distance] floatValue];
    NSInteger hotelPrice = [[[TableArray objectAtIndex:i]xmlhotel_price] integerValue];

    sPredicate = [NSPredicate predicateWithFormat:@"(%ld <= %d AND %f <= %d)" , (long)hotelPrice, numberOfBudget, hotelDistanceFloat,numberOfDistance];
    TableArray = [[TableArray filteredArrayUsingPredicate:sPredicate] mutableCopy];
}

您应该使用包含字典的数组并将谓词中的过滤器应用于该数组。应该不需要循环来对内容进行排序。

一旦你得到过滤后的数组,就会重新加载表格。

【讨论】:

  • 你能解释一下我该怎么做吗
【解决方案3】:

你的错

  1. 您在枚举数组时过滤数组。
  2. 谓词值从要过滤的数组中选择。

你应该怎么做:

  1. 从输入 (hotelDistanceFloat,numberOfDistance) 中获取谓词的值。
  2. 应用谓词如:

_

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"(hotelPrice <= %d AND numberOfBudget <= %d)" , hotelDistanceFloat,numberOfDistance];

TableArray = [[TableArray filteredArrayUsingPredicate:sPredicate].mutableCopy]

【讨论】:

  • 我应该把这段代码放在哪里??它将如何获得hotelPrice?如果我这样说?
  • 您需要从设置过滤器的输入中获取过滤值。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 2023-03-26
  • 2013-04-16
  • 2015-04-06
相关资源
最近更新 更多