tags:

views:

43

answers:

1

i use Core Data to store datas, and i have got 2 entities called "train" and "airplane". i fetched all of items into the two different NSMutableArray (one of the trains and one of the aircrafts). each class has NSDate attribute and i want to show all items from arrays in only one (single) UITableViewController sorted by date. furthermore i defined own tableCells class to handle informations.

my question is how can i show different classed objects in single UITableViewController sorted by date ?

A: 

I have no experience with CoreDate, but may be NSFetchedResultsController class will be suitable for your task.

If you don't want to show your different entities in separate table sections I think the easiest way to mix them will be to use single array to store and handle your data.

To make things easier you can declare a protocol with methods common for your entities (e.g. date attribute) and make your entities conform it:

@protocol MyEntityProtocol    
- (NSDate*) date;
@end
  1. Sorting: You can sort your array for example using sortedArrayUsingFunction:context: function.

    NSInteger entityDateSort(id<MyEntityProtocol> obj1, id<MyEntityProtocol> obj2, void *context){      
        return [[obj1 date] compare:[obj2 date]];
    }
    
  2. Displaying in table. If you have different types of cell for your entities:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        id ent = [dataArray objectAtIndex:indexPath.row];
        UITableViewCell* cell = nil;
        if ([ent isKindOfClass:[Entity1 class]]){
            // create and/or setup cell for entity1
        }
        if ([ent isKindOfClass:[Entity2 class]]){
             // create and/or setup cell for entity2
        }
        return cell;
    }
    
Vladimir
sounds/looks great thank you very much
Victor