TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
GTTableViewController is a light weight subclass of UIViewController
that integrates NSFetchedResultsController
and UITableView
to display data of CoreData.
It has a good scalability, and can be integrated to your project either by inheritance or as a child viewController using datasource methods.
To display the data in UITableView, all you need to do is to customize your own NSFetchRequest
and UITableViewCell
objects.
Write a GTTableViewController subclass and override its methods like this:
Return NSManagedObjectContext
object which CoreData context you want to use. Default returns from AppDelegate, If you do not override it.
- (NSManagedObjectContext *)managedObjectContext
{
id appDelegate = [UIApplication sharedApplication].delegate;
return [appDelegate managedObjectContext];
}
Retrun NSFetchRequest
object include the fetch rules and orders. This method must be overrided.
- (NSFetchRequest *)fetchRequest
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *playerEntity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:playerEntity];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
return fetchRequest;
}
Config your own UITableViewCell
. Cell is empty, if you do not override it.
- (void)configCell:(UITableViewCell *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath fetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
{
Player *player = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = player.name;
}
Return your own UITableViewCell
. Cell is UITableViewCellStyleDefault
, if you do not override it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath fetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[self configCell:cell cellForRowAtIndexPath:indexPath fetchedResultsController:fetchedResultsController];
return cell;
}
DataSource is almost like Inheritance way. This way is used for integrating it as a subview or a child viewController.
In UIViewController
object, write code like:
GTTableViewController *viewController = [[GTTableViewController alloc] init];
viewController.dataSource = self;
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
- (NSManagedObjectContext *)managedObjectContextGTTableViewController:(GTTableViewController *)viewController
This method is requeired.
- (NSFetchRequest *)fetchRequestGTTableViewController:(GTTableViewController *)viewController
- (void)configCell:(UITableViewCell *)cell viewController:(GTTableViewController *)viewController fetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
- (void)configCell:(UITableViewCell *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath viewController:(GTTableViewController *)viewController fetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
Fetch objects method
- (void)performFetch
This property controls the max number of displayed fetched objects in UITableView.
@property (nonatomic, assign) int numberOfFetchLimit
龚涛 Gong Tao mail: [email protected]