简体   繁体   中英

Inserting TableViewCells based on Values of other Tableview Cells

I am creating a flight track app that is a tableview loaded from core data and I need to have a row appear if the arrival airport of one flight and the departure airport of another flight are the same, and have this tableview display the time difference. For each flight I have core data attributes of arrivaltime, departuretime, arrivalairport, and departureairport. I am unsure of how to load this in a way to utilize these attributes. Once I have loaded them what methods should I be looking at?

The flights will all be associated with a trip number which is another core data attribute, so this logic is only needed between associated trips. For example if trip 1 has flight 1 from airport 1 to airport 2 and flight 2 from airport 2 to airport 3, then I need the time between flight 1 arriving and flight 2 departing.

Any help or pointers are appreciated. This is the code I have to load the core data attributes in viewdidappear

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors         = [[NSArray alloc] initWithObjects:sortDescriptor, nil];


// Set descriptors
[fetchRequest setSortDescriptors:sortDescriptors];

self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

The following is how the tableview cells are displayed in cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];

[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]]];
[cell.detailTextLabel setText:[device valueForKey:@"company"]];

 thearray=[device valueForKey:@"name"];

Run a fetch using your trip number to get all of the flights in the trip. Sort the flights based on the date of departure.

Now, you can iterate through the array returned by the fetch and compare each pair of items to see if the destination of one is the same as the departure of the next.

What you decide to do now depends very much on how you're filling the table view with data (using an FRC) and how things should be displayed... If you're using an array of flights then you can add a new item into the array which will cause you to create a different type of cell. If you're using an FRC then things could be interesting. In the FRC case it's probably best not to use an additional row, but to use a taller cell which includes additional information about the connection time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM