Vix Hunk Vix Hunk - 4 months ago 19
Objective-C Question

App crashes after reloading tableview data after NSPredicate

UITableView
loads data perfectly at first. when i apply
NSPredicate
on
NSMutableDictionary
*list and table reloads the data, after that on scroll the app is crashing. I guess major problem is in
numberOfRowsInSection
. Here is my code

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"];


and here is method for filtering data.

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


data filters perfectly but when uitableviewscroll and reaches at the end of the table app crashes.
other methods

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


error log

enter image description here

Answer

The issue you are facing is because you have two different array, and both have different filter on it, that will cause issue, Try to use single array, that contains dictionary object like this. So change your code like this.

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