views:

113

answers:

1

This has been bugging me for hours now and i have not been able to figure it out.

I am importing data into a tableview using core data and NSMutableArray. As shown below.

CORE DATA ARRAY

NSMutableArray *mutableFetchResults  = [CoreDataHelper getObjectsFromContext:@"Spot" :@"Name" :YES :managedObjectContext];
self.entityArray = mutableFetchResults;

TABLE VIEW

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];

    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }



    NSString *lat1 = [object valueForKey:@"Email"];

    //NSLog(@"Current Spot Latitude:%@",lat1);

    float lat2 = [lat1 floatValue];
    //NSLog(@"Current Spot Latitude Float:%g", lat2);

    NSString *long1 = [object valueForKey:@"Description"];

    //NSLog(@"Current Spot Longitude:%@",long1);

    float long2 = [long1 floatValue];
    //NSLog(@"Current Spot Longitude Float:%g", long2);

    //Getting current location from NSDictionary

    CoreDataTestAppDelegate *appDelegate = (CoreDataTestAppDelegate *) [[UIApplication sharedApplication] delegate];
    NSString *locLat = [NSString stringWithFormat:appDelegate.latitude];
    float locLat2 = [locLat floatValue];
    //NSLog(@"Lat: %g",locLat2);

    NSString *locLong = [NSString stringWithFormat:appDelegate.longitude];
    float locLong2 = [locLong floatValue];
    //NSLog(@"Long: %g",locLong2);

    //Distance Shizzle
    //Prime's Location
    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
    //Home Location
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:locLat2 longitude:locLong2];

    double distance = [loc1 getDistanceFrom: loc2] / 1000;

    int myInt = (int)(distance + (distance>0 ? 0.5 : -0.5));

    //NSLog(@"INT VAL :%i", myInt);

    NSMutableString* converted = [NSMutableString stringWithFormat:@"%.1f", distance];

    [converted appendString: @" Km"];

    //NSLog(@"Distance between Prime and home = %g", converted);

    if (myInt < 11) {
        cell.textLabel.text = [object valueForKey:@"Name"];
        cell.detailTextLabel.text = [NSString stringWithFormat:converted];
    }
    else {


    }

    // Configure the cell...

    return cell;
}

I am trying to get the table only to display results that are within a certain distance. This method here works apart from the fact that the results over a certain distance are still in the table, they are just not graphically visible.

I am led to believe that i have to carry out the filtering process before the formatting the table but i can not seem to do this.

Please help. My xcode skills are not brilliant so code suggestions would be helpfull.

+2  A: 

You don't want to be trying to filter the results in cellForRowAtIndexPath:, because there isn't a direct correspondence between the index passed to cellForRowAtIndexPath: and the entries in you entityArray. For example, if your entire entityArray has 10 elements, but only 3 are within the desired distance, indexes into entityArray will go from 0..9, but only cells 0..2 in your tableview will exist.

You should create a separate, filtered, NSMutableArray at the time you fetch your results, and display the contents of that array in your TableView.

The other reason you need to do your filtering in advance is that you need to implement the -numberOfRowsInSection: method in your tableview controller, to tell the tableview how many rows there are. You obviously can't do what without doing the filtering.

Edit:

You could create the filtered array doing something like:

@property (nonatomic, retain) NSMutableArray *filteredArray;

self.filteredArray = [NSMutableArray array]; // start w/ empty array

for (MyObject *obj in self.entityArray) { // use fast enumeration to look at each element
    if ([obj shouldBeAdded]) { // or whatever function you use to test distance
         [self.filteredArray addObject:obj]; // add objects that match
    }
}

(and don't forget to release filteredArray in your -dealloc method)

David Gelhar
I understand this but how do i go about creating the filtered array?
James
I LOVE YOU! Works like a dream
James
@James: While expressions of love are great, the standard way to thank someone for their input is to accept their answer as correct. :)
Matt B.