我想重写我在应用中制作的视图以改善布局。最初我实现了一个使用CoreData作为源的TableViewController。这个视图对我来说效果很好,直到我需要在TableViewController中添加更多视图,这些视图将独立于表,并且在添加更多行时不会滚出屏幕。基于NSFetchedResultsController的苹果指南:
,初步实现如下CoreDataTableViewController.h:
@interface CoreDataTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
- (void)performFetch;
@property (nonatomic) BOOL suspendAutomaticTrackingOfChangesInManagedObjectContext;
@property BOOL debug;
@end
CoreDataTableViewController.m:
@interface CoreDataTableViewController()
@property (nonatomic) BOOL beganUpdates;
@end
@implementation CoreDataTableViewController
#pragma mark - Properties
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize suspendAutomaticTrackingOfChangesInManagedObjectContext = _suspendAutomaticTrackingOfChangesInManagedObjectContext;
@synthesize debug = _debug;
@synthesize beganUpdates = _beganUpdates;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark - Fetching
- (void)performFetch
{
<snipped>
}
- (void)setFetchedResultsController:(NSFetchedResultsController *)newfrc
{
<snipped>
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.fetchedResultsController sectionIndexTitles];
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
<..snipped..>
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
<...snipped...>
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
}
- (void)endSuspensionOfUpdatesDueToContextChanges
{
}
- (void)setSuspendAutomaticTrackingOfChangesInManagedObjectContext:(BOOL)suspend
{
}
@end
在我的TableViewController,名为CDTVC.h中,我创建了一个继承的子类 来自CoreDataTableViewController:
@interface CDTVC : CoreDataTableViewController
@property (nonatomic, copy) NSString *status;
@property (nonatomic, copy) NSString *totalTally;
@property (nonatomic, retain) IBOutlet UILabel *tallyDisplayLabel;
@end
在我的CDTVC.m中:
// Gets called when the managedObjectContext is ready
-(void) loadTally
{
if (managedObjectContext) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entry"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dateCreated" ascending:NO]];
request.predicate = nil;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
} else {
self.fetchedResultsController = nil;
}
[self performFetch]; // <<----This refreshes the tableview controller
}
根据指南,我需要自己实现,因为它不是由处理 父:
#pragma mark - CoreDataViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"entryCRVPCell"];
if (cell == nil) {
UITableViewCellStyle cellStyle = UITableViewCellStyleSubtitle;
cell = [[UITableViewCell alloc] initWithStyle:cellStyle reuseIdentifier:@"entryCRVPCell"];
}
TallyEntry *tallyEntry = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (tallyEntry) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:tallyEntry.dateCreated];
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@\n%@", tallyEntry.category, tallyEntry.name, stringDate];
}
return cell;
}
所以现在我的视图控制器不再是TableViewController,而是一个包含TableView的ViewController:
所以我介绍了一个新的View Controller,这就是我觉得我所做的 一个黑客,并不像解决方案那样优雅:
在CDTVC.h中,我添加了以下内容,以便我可以访问数据 TallyViewController:
@protocol TallyViewDataSource
-(void)setupTallyLabel:(float) tally;
-(NSString *) getTallyMode;
@end
@property (nonatomic, retain) IBOutlet id <TallyViewDataSource> dataSource;
在CDTVC.m中,我添加了以下内容:
@synthesize dataSource = _dataSource;
我将loadTally实现移动到TallyViewController。
TallyViewController.h现在必须从UIViewController继承,这意味着我 不能继承CoreDataViewController:
@interface TallyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,
TallyViewDataSource> {
IBOutlet UITableView* myTableView;
}
@property (nonatomic, retain) IBOutlet UILabel *tallyDisplayLabel;
@property (nonatomic, retain) IBOutlet UITableView *myTableView;
@property (nonatomic, retain) CDTVC* CDTVCTable;
因此,为了解决这个多重继承问题,我决定分配我的 在我的TallyViewController.m中拥有自己的CDTVCTable实例:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_CDTVCTable = [[CDTVC alloc] init];
_CDTVCTable.dataSource = self;
}
-(void) loadTally
{
if (managedObjectContext) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entry"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dateCreated" ascending:NO]];
request.predicate = nil;
_CDTVCTable.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
} else {
_CDTVCTable.fetchedResultsController = nil;
}
[_CDTVCTable performFetch];
[self.myTableView reload]; //<----Had to add this to refresh the local table.
}
我还必须实现所有表视图委托,但是将它们重定向到 我的CDTVCTable,和以前一样我只需要实现tableView: 的cellForRowAtIndexPath:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_CDTVCTable numberOfSectionsInTableView:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_CDTVCTable tableView:tableView numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [_CDTVCTable tableView:tableView cellForRowAtIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
return [_CDTVCTable tableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath];
}
所以问题是,是否有一种更优雅的方式来实现它 代表和数据来源。我必须承认我对代表和我的把握 数据源很弱,所以我不确定以更好的方式概述这一点。
答案 0 :(得分:0)
如果您需要/想要将静态视图元素与UITableview混合,那么我发现通常最简单的方法是将容器视图添加到ViewController并将外部UITableview连接到它。
通过Segue将静态ViewController的转发数据发送到UITableView,然后使用委托让静态ViewController知道UITableview中发生了什么。