在NSPredicate之后重新加载tableview数据后应用程序崩溃

时间:2016-08-06 04:52:13

标签: ios objective-c uitableview nspredicate

UITableView首先完美地加载数据。当我在NSPredicate *列表上应用NSMutableDictionary并且表重新加载数据时,在滚动之后应用程序崩溃。我想主要问题在于numberOfRowsInSection。这是我的代码

viewDidLoad中

NSArray *allstartingPrice = @[@100,@200,@400,@500,@300,@100,@1000,@1000,@200,@700];
NSArray *alldistanceFrom = @[@7,@9,@8,@3,@30,@35,@50,@10,@15,@25];

NSAray *startingPrice = allstartingPrice;
NSAray *distanceFrom = alldistanceFrom;

list = [[NSMutableDictionary alloc] init];
[list setObject:startingPrice forKey:@"startingfrom"];
[list setObject:distanceFrom forKey:@"distance"];

这是过滤数据的方法。

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    NSArray * allKeys = [list allKeys];
    return [allKeys count];
}

- (void) sortTable{
    int match = numberOfBudget;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF <= %i", match];
    startingPrice = [allstartingPrice filteredArrayUsingPredicate:predicate];

    int match2 = numberOfDistance;
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF <= %i", match2];
    distanceFrom = [alldistanceFrom filteredArrayUsingPredicate:predicate2];

    list = [[NSMutableDictionary alloc] init];
    [list setObject:startingPrice forKey:@"startingfrom"];
    [list setObject:distanceFrom forKey:@"distance"];

    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
}

数据过滤完美,但当uitableviewscroll并在表格末尾到达应用程序崩溃。 其他方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{

    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FSParallaxTableViewCell *cell = (FSParallaxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.cellImageView.image = [UIImage imageNamed:[[list objectForKey:@"imagesnames"] objectAtIndex:indexPath.row]];
    cell.cellImageView.contentMode = UIViewContentModeScaleAspectFill;

    cell.backgroundColor = [UIColor clearColor];

    UIImage *img = [UIImage imageNamed:@"flat_color.png"];
    cell.startingat.backgroundColor = [UIColor colorWithPatternImage:img];
    cell.startingat.font = [UIFont fontWithName:@"HelveticaNeue-light" size:12];
    cell.startingat.textColor = [UIColor blackColor];
    cell.startingat.adjustsFontSizeToFitWidth = YES;
    UIImage *img2 = [UIImage imageNamed:@"flat_color.png"];
    cell.distance.backgroundColor = [UIColor colorWithPatternImage:img2];
    cell.distance.textAlignment = NSTextAlignmentCenter;
    cell.distance.font = [UIFont fontWithName:@"HelveticaNeue-light" size:9];
    cell.distance.textColor = [UIColor whiteColor];
    cell.distance.adjustsFontSizeToFitWidth = YES;
    NSString *dis = [[NSString alloc] initWithFormat:@"%@ km", [[list objectForKey:@"distance"] objectAtIndex:indexPath.row]];
    return cell;
}

错误日志

enter image description here

1 个答案:

答案 0 :(得分:0)

你面临的问题是因为你有两个不同的数组,并且两者都有不同的过滤器,这将导致问题,尝试使用包含这样的字典对象的单个数组。所以改变这样的代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSArray *allstartingPrice = @[@100,@200,@400,@500,@300,@100,@1000,@1000,@200,@700];
    NSArray *alldistanceFrom = @[@7,@9,@8,@3,@30,@35,@50,@10,@15,@25];

    array = [[NSMutableArray alloc] init];
    for (NSInteger i=0; i<allstartingPrice.count; i++) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setValue:[allstartingPrice objectAtIndex:i] forKey:@"startingfrom"];
        [dic setValue:[alldistanceFrom objectAtIndex:i] forKey:@"distance"];
    }
}   

- (void) sortTable{
    //int match = numberOfBudget;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startingfrom <= %d && distance <= %d", 100, 10];
    filteredArray = [NSMutableArray arrayWithArray:[array filteredArrayUsingPredicate:predicate]];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (filteredArray.count > 0) {
        return filteredArray.count;
    }
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic;
    if (filteredArray.count > 0) {
        dic = [filteredArray objectAtIndex:indexPath.row];
    }
    else {
        dic = [array objectAtIndex:indexPath.row];
    }
    FSParallaxTableViewCell *cell = (FSParallaxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.cellImageView.image = [UIImage imageNamed:[[list objectForKey:@"imagesnames"] objectAtIndex:indexPath.row]];
    cell.cellImageView.contentMode = UIViewContentModeScaleAspectFill;

    cell.backgroundColor = [UIColor clearColor];

    UIImage *img = [UIImage imageNamed:@"flat_color.png"];
    cell.startingat.backgroundColor = [UIColor colorWithPatternImage:img];
    cell.startingat.font = [UIFont fontWithName:@"HelveticaNeue-light" size:12];
    cell.startingat.textColor = [UIColor blackColor];
    cell.startingat.adjustsFontSizeToFitWidth = YES;
    cell.startingat.text = [dic valueForKey:@"startingfrom"];
    UIImage *img2 = [UIImage imageNamed:@"flat_color.png"];
    cell.distance.backgroundColor = [UIColor colorWithPatternImage:img2];
    cell.distance.textAlignment = NSTextAlignmentCenter;
    cell.distance.font = [UIFont fontWithName:@"HelveticaNeue-light" size:9];
    cell.distance.textColor = [UIColor whiteColor];
    cell.distance.adjustsFontSizeToFitWidth = YES;
    cell.distance.text = [dic valueForKey:@"distance"];
    return cell;
}