how can i fill UITableViewController from two different data source ? (iPhone)
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 UITableViewContro开发者_C百科ller sorted by date ?
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
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]]; }
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; }
精彩评论