我正在制作一个包含MagicalRecord和Core Data的简单小iOS应用。但是我对故事板和应用程序之间的差异感到困惑。我的两个小节按钮(状态和+)没有按预期显示。无论我在视图的标题中键入什么,它总是显示“Home”(这是原始文本)。此外,我最初有一个tableview但删除它。尽管如此,它仍然显得有点歪斜,如附带的屏幕截图所示。
我会提到另一个可能的线索。我不明白它为什么存在,我也不明白它是如何导致这个问题的。事实上,我打算把它作为一个单独的问题。我在这里提到它只是因为一个更有经验的开发人员可能认为是嫌疑人是一个谜。这是从控制台:
2014-01-01 22:37:50.691 WMLG1[32260:a0b] CoreData: warning: no NSValueTransformer with class name 'ImageToDataTransformer' was found for attribute 'image' on entity 'Image'
2014-01-01 22:37:50.693 WMLG1[32260:a0b] CoreData: warning: no NSValueTransformer with class name 'ImageToDataTransformer' was found for attribute 'thumbnailImage' on entity 'Recipe'
2014-01-01 22:37:50.724 WMLG1[32260:a0b] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x2f30ac) -> Created Context UNNAMED
2014-01-01 22:37:50.724 WMLG1[32260:a0b] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x2f30ac) Set Root Saving Context: <NSManagedObjectContext: 0x8a47b60>
2014-01-01 22:37:50.726 WMLG1[32260:a0b] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x2f30ac) Created Main Queue Context: <NSManagedObjectContext: 0x8a481b0>
2014-01-01 22:37:50.727 WMLG1[32260:a0b] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x2f30ac) Set Default Context: <NSManagedObjectContext: 0x8a481b0>*
这对我来说很神秘的原因是我的模型中没有名为“Image”或“Recipe”的实体。
我很绿,所以一切皆有可能,虽然我非常小心,并且觉得我对代码有很好的把握(虽然我相信可能会有一些冗余)。有人可以指出我做错了什么吗?
以下是HomeViewController的相关代码:
//
// HomeViewController.m
// WMLG1
//
// Created by Tim Jones on 1/1/14.
// Copyright (c) 2014 TDJ. All rights reserved.
//
#import "HomeViewController.h"
#import "ListActivity.h"
@interface HomeViewController ()
{
NSFetchedResultsController * frc;
}
@end
@implementation HomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Table View stuff
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:NO];
return [[frc sections ]count];
}
- (NSInteger)sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)sectionIndex
{
frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:NO];
return [frc sectionForSectionIndexTitle:@"category" atIndex:sectionIndex];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:NO];
NSInteger count = 0;
NSInteger realNumberOfSections = [frc.sections count];
if (section < realNumberOfSections)
{
// fetchedResultsController has this section
id <NSFetchedResultsSectionInfo> sectionInfo = [frc.sections objectAtIndex:section];
count = [sectionInfo numberOfObjects];
}
else
{
// section not present in fetchedResultsController
count = 0; // for empty section, or 1 if you want to show a "no objects" cell.
}
return count;}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:NO];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Check if a reusable cell object was dequeued
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Populate the cell with the appropriate name based on the indexPath
cell.textLabel.text = [frc.sections objectAtIndex:indexPath];
return cell;
}
#pragma mark Table View Delegate stuff
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
答案 0 :(得分:0)
从模拟器中删除应用程序,从屏幕顶部的栏中选择“硬件”,然后选择“主页”。单击并按住您的应用程序图标,直到出现一点“X”,然后单击它以删除该应用程序。在Xcode中,转到顶部的“Product”,然后单击“Clean”。然后再次尝试在模拟器上运行应用程序。
Home View Controller是初始视图控制器吗?
您在Home View Controller中实现了UITableView控件方法,但询问了是否显示了UITableView。该控制器应该是UITableViewController还是仅仅是委托?