我四处搜寻但找不到任何特定于我特定问题的内容。我有一个应用程序,将一组已保存的游戏对象加载到UITableview。除非我尝试点击过去的游戏加载将显示数据的视图,否则我可以保存并绕过应用程序的所有其他部分。我使用了这个教程:(http://timroadley.com/2012/02/12/core-data-basics-part-2-core-data-views/)并将其改编为我的代码并让它在我工作时工作只需将我的游戏名称加载到随机标签中即可。我所做的重大改变是,点击单元格后应该出现的过去游戏记分卡视图不再是tableview的委托。我还添加了更多属性,并在执行此操作后刷新了NSManagedObject子类。如果我需要显示更多代码,请告诉我。
这是TableView的.m代码:
#import "PastGames.h"
#import "Game.h"
@interface PastGames ()
@end
@implementation PastGames
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize selectedGame;
- (void)setupFetchedResultsController
{
// 1 - Decide what Entity you want
NSString *entityName = @"Game"; // Put your entity name here
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter it if you want
//request.predicate = [NSPredicate predicateWithFormat:@"Role.name = Blah"];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setupFetchedResultsController];
}
/*
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Game Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Game *game = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = game.name;
cell.detailTextLabel.text = game.date;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException
// Delete the role object that was swiped
Game *gameToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting (%@)", gameToDelete.name);
[self.managedObjectContext deleteObject:gameToDelete];
[self.managedObjectContext save:nil];
// Delete the (now empty) row on the table
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self performFetch];
[self.tableView endUpdates];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Show Game Segue"])
{
NSLog(@"Setting PastGames as a delegate of PastGameScoreCard");
PastGameScoreCard *pastGameScoreCard = segue.destinationViewController;
pastGameScoreCard.managedObjectContext = self.managedObjectContext;
// Store selected Game in selectedGame property
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
self.selectedGame = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Passing selected game (%@) to PastGameScoreCard", self.selectedGame.name);
pastGameScoreCard.game = self.selectedGame;
NSLog(@"Success!");
}
else
{
NSLog(@"Unidentified Segue Attempted!");
}
}
@end
这是应该出现的视图的.m文件,并从该游戏中加载数据,PastGameScoreCard:
#import "PastGameScoreCard.h"
@interface PastGameScoreCard ()
@end
@implementation PastGameScoreCard
//Player Names
@synthesize frontOrBack;
@synthesize holeNumbers;
@synthesize p1NameLabel;
@synthesize p2NameLabel;
@synthesize p3NameLabel;
@synthesize p4NameLabel;
//Player 1
@synthesize p1Hole1and10Label;
@synthesize p1Hole2and11Label;
@synthesize p1Hole3and12Label;
@synthesize p1Hole4and13Label;
@synthesize p1Hole5and14Label;
@synthesize p1Hole6and15Label;
@synthesize p1Hole7and16Label;
@synthesize p1Hole8and17Label;
@synthesize p1Hole9and18Label;
@synthesize p1TotalLabel;
//Player 2
@synthesize p2Hole1and10Label;
@synthesize p2Hole2and11Label;
@synthesize p2Hole3and12Label;
@synthesize p2Hole4and13Label;
@synthesize p2Hole5and14Label;
@synthesize p2Hole6and15Label;
@synthesize p2Hole7and16Label;
@synthesize p2Hole8and17Label;
@synthesize p2Hole9and18Label;
@synthesize p2TotalLabel;
//Player 3
@synthesize p3Hole1and10Label;
@synthesize p3Hole2and11Label;
@synthesize p3Hole3and12Label;
@synthesize p3Hole4and13Label;
@synthesize p3Hole5and14Label;
@synthesize p3Hole6and15Label;
@synthesize p3Hole7and16Label;
@synthesize p3Hole8and17Label;
@synthesize p3Hole9and18Label;
@synthesize p3TotalLabel;
//Player 4
@synthesize p4Hole1and10Label;
@synthesize p4Hole2and11Label;
@synthesize p4Hole3and12Label;
@synthesize p4Hole4and13Label;
@synthesize p4Hole5and14Label;
@synthesize p4Hole6and15Label;
@synthesize p4Hole7and16Label;
@synthesize p4Hole8and17Label;
@synthesize p4Hole9and18Label;
@synthesize p4TotalLabel;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize game = _game;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(@"Setting the value of fields in this static table to that of the passed Game");
//Player 1
self.p1NameLabel.text = self.game.p1Name;
self.p1Hole1and10Label.text = self.game.p1h1;
self.p1Hole2and11Label.text = self.game.p1h2;
self.p1Hole3and12Label.text = self.game.p1h3;
self.p1Hole4and13Label.text = self.game.p1h4;
self.p1Hole5and14Label.text = self.game.p1h5;
self.p1Hole6and15Label.text = self.game.p1h6;
self.p1Hole7and16Label.text = self.game.p1h7;
self.p1Hole8and17Label.text = self.game.p1h8;
self.p1Hole9and18Label.text = self.game.p1h9;
self.p1TotalLabel.text = self.game.p1Total;
//Player 2
self.p2NameLabel.text = self.game.p2Name;
self.p2Hole1and10Label.text = self.game.p2h1;
self.p2Hole2and11Label.text = self.game.p2h2;
self.p2Hole3and12Label.text = self.game.p2h3;
self.p2Hole4and13Label.text = self.game.p2h4;
self.p2Hole5and14Label.text = self.game.p2h5;
self.p2Hole6and15Label.text = self.game.p2h6;
self.p2Hole7and16Label.text = self.game.p2h7;
self.p2Hole8and17Label.text = self.game.p2h8;
self.p2Hole9and18Label.text = self.game.p2h9;
self.p2TotalLabel.text = self.game.p2Total;
//Player 3
self.p3NameLabel.text = self.game.p3Name;
self.p3Hole1and10Label.text = self.game.p3h1;
self.p3Hole2and11Label.text = self.game.p3h2;
self.p3Hole3and12Label.text = self.game.p3h3;
self.p3Hole4and13Label.text = self.game.p3h4;
self.p3Hole5and14Label.text = self.game.p3h5;
self.p3Hole6and15Label.text = self.game.p3h6;
self.p3Hole7and16Label.text = self.game.p3h7;
self.p3Hole8and17Label.text = self.game.p3h8;
self.p3Hole9and18Label.text = self.game.p3h9;
self.p3TotalLabel.text = self.game.p3Total;
//Player 4
self.p4NameLabel.text = self.game.p4Name;
self.p4Hole1and10Label.text = self.game.p4h1;
self.p4Hole2and11Label.text = self.game.p4h2;
self.p4Hole3and12Label.text = self.game.p4h3;
self.p4Hole4and13Label.text = self.game.p4h4;
self.p4Hole5and14Label.text = self.game.p4h5;
self.p4Hole6and15Label.text = self.game.p4h6;
self.p4Hole7and16Label.text = self.game.p4h7;
self.p4Hole8and17Label.text = self.game.p4h8;
self.p4Hole9and18Label.text = self.game.p4h9;
self.p4TotalLabel.text = self.game.p4Total;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
/*
//When view appears, show front 9 scores
- (void)viewDidAppear: (BOOL)animated
{
[super viewDidAppear: animated];
//Sets name of course in game object to title.
self.title = self.game.name;
}
*/
- (void)viewDidUnload
{
[self setP1Hole4and13Label:nil];
[self setP1Hole5and14Label:nil];
[self setP1Hole6and15Label:nil];
[self setP1Hole7and16Label:nil];
[self setP1Hole8and17Label:nil];
[self setP1Hole9and18Label:nil];
[self setP1TotalLabel:nil];
[self setP2Hole1and10Label:nil];
[self setP2Hole2and11Label:nil];
[self setP2Hole3and12Label:nil];
[self setP2Hole4and13Label:nil];
[self setP2Hole5and14Label:nil];
[self setP2Hole6and15Label:nil];
[self setP2Hole7and16Label:nil];
[self setP2Hole8and17Label:nil];
[self setP2Hole9and18Label:nil];
[self setP2TotalLabel:nil];
[self setP3Hole1and10Label:nil];
[self setP3Hole2and11Label:nil];
[self setP3Hole3and12Label:nil];
[self setP3Hole4and13Label:nil];
[self setP3Hole5and14Label:nil];
[self setP3Hole6and15Label:nil];
[self setP3Hole7and16Label:nil];
[self setP3Hole8and17Label:nil];
[self setP3Hole9and18Label:nil];
[self setP3TotalLabel:nil];
[self setP4Hole1and10Label:nil];
[self setP4Hole2and11Label:nil];
[self setP4Hole3and12Label:nil];
[self setP4Hole4and13Label:nil];
[self setP4Hole5and14Label:nil];
[self setP4Hole6and15Label:nil];
[self setP4Hole7and16Label:nil];
[self setP4Hole8and17Label:nil];
[self setP4Hole9and18Label:nil];
[self setP4TotalLabel:nil];
[self setHoleNumbers:nil];
[self setFrontOrBack:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
同样,当我点击保存了游戏的单元格时,应用程序才会崩溃,并且应该显示一个记分卡视图,其中所有数据都会从保存的核心数据中加载到标签中。