iPhone UITableView 怪异

发布于 2024-11-15 02:23:42 字数 1599 浏览 3 评论 0原文

我什至不知道该怎么称呼这个问题。更不用说如何寻找解决方案了。

我正在使用 Xcode 4。 我正在使用 CoreData。 我有标签栏应用程序。 4 个不同的选项卡。 这是模拟器的问题。

其中两个选项卡的根表视图由控制器数组填充。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];

SecondViewController *nextController = [self.medControllersArray objectAtIndex:row];
[self.navigationController pushViewController:nextController
                                     animated:YES];
}

当深入到其中一个控制器时,您可以看到另一个表视图。

当点击下一个控制器的表视图中的“添加”按钮并添加名称或其他内容来填充表视图时,如果您转到另一个下一个控制器的表视图,它将填充相同的信息和另一个表视图。

由于我遇到了臭名昭著的 +entityForName 错误,所以

我在 viewDidLoad 中添加了此内容,

 if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(MIT2AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    NSLog(@"After managedObjectContext_: %@",  managedObjectContext);
}

在应用程序的委托方法中添加了此内容 application:didFinishLaunchingWithOptions

ViewController *viewController = [[ViewController alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
    NSLog(@"\nCould not create *context for self");
}

viewController.managedObjectContext = context;

我为每个 next 我拥有的视图控制器和两个选项卡的视图,其根视图未由控制器数组填充。

当推送下一个视图时,我还会在控制台中收到此警告:

在 ManagedObjectContext_:  之后

我希望这一切都有道理。有人可以为我指明正确的方向。

提前致谢。

I'm not even sure what to call this problem. Much less how to search for a solution.

I'm using Xcode 4.
I'm using CoreData.
I have tab bar app. 4 different tabs.
This is an issue from the simulator.

The root table view for two of the tab is populated by an array of controllers.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];

SecondViewController *nextController = [self.medControllersArray objectAtIndex:row];
[self.navigationController pushViewController:nextController
                                     animated:YES];
}

When drilling down to one of the controllers, there's another table table view as you can see.

When tapping the Add button in the next controller's table view and adding a name or whatever to populate the table view, if you go to another next controller's table view it is populated with the same info and the other table view.

Since I was getting the infamous +entityForName error

I add this in viewDidLoad

 if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(MIT2AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    NSLog(@"After managedObjectContext_: %@",  managedObjectContext);
}

Add this in the app's delegate method application:didFinishLaunchingWithOptions

ViewController *viewController = [[ViewController alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
    NSLog(@"\nCould not create *context for self");
}

viewController.managedObjectContext = context;

I did this for each next view controller I had and the root views for the two tabs whose root view isn't populated by and array of controllers.

I also get this warning in the console when a next view is pushed:

After managedObjectContext_: <NSManagedObjectContext: 0x5934e10>

I hope it all makes sense. And that someone can point me in the right direction.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-11-22 02:23:42

您是否尝试过从模拟器中删除该应用程序?有时,当您更改核心数据结构上的某些内容时,应用程序会因无法正确找到数据而崩溃。删除该应用程序将强制其重建数据库。

have you tried to remove the app from the simulator? sometimes when you change something on the core data structure the app crashes at it fails to find the data correctly. Removing the app will force it to rebuild the database.

护你周全 2024-11-22 02:23:42

感谢上帝!!!已经解决了。我在我的数据模型中犯了(我确信)一个新手核心数据错误。

由于这个错误,我在模拟器(以及手机)中得到了重复项:

在数据模型中,我有一个药物实体和所有属性,等等,等等。我创建了一个类,并决定仅使用该类在 3 个表视图、当前药物、过去药物和过敏之间共享。这样做自然没有区别。我回去,创建了一个新模型,合并了yada、yada,并添加了当前、过去和过敏三个新实体。然后将父实体设置为主要医疗实体并抽象父实体。当我再次检查并将代码修改为新实体时。一切顺利!不再有重复。好吧,真的,不再需要三倍了。

对于我收到的警告(不是错误,应用程序仍在构建,没有崩溃,您仍然可以解决它)是对托管对象上下文执行的。为了消除臭名昭著的 +entityForName 错误,我尝试了在书中或苹果公司看到的一些方法。我将 AppDelegate 导入到每个表中,并在 fetchedResultsController 中执行了以下操作:

- (NSFetchedResultsController *)fetchedResultsController {
MIT2AppDelegate *mit = [[MIT2AppDelegate alloc] init];

if (fetchedResultsController != nil) {
    return fetchedResultsController;
}
    //if (fetchedResultsController == nil) {
    // Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];


    //  the entity name
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Allergies" inManagedObjectContext:mit.managedObjectContext];
[fetchRequest setEntity:entity];

在重新编写代码时,我尝试了 tomasBULL 推荐的内容:
如何解决 NSInternalInconsistencyException',原因:'+entityForName:失败报告< /a>

这就是错误的来源。所以我又回到了之前的状态,现在不再有错误了。

我在我的 AppDelegate ManagedObjectModel 中执行此操作,

- (NSManagedObjectModel *)managedObjectModel{
    if (managedObjectModel != nil){
        return managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle]  pathForResource:@"MIT2ver 3" ofType:@"mom" inDirectory:@"MIT2ver2.momd"];

        NSURL *momURL = [NSURL fileURLWithPath:path];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return managedObjectModel;
}

因为在添加模型时我犯了另一个菜鸟错误,我删除了空间。感谢udibr的回答:核心的“自动轻量级迁移”的实现数据(iPhone)

感谢所有回答的人。上帝解决了!

Thank GOD!!! It's worked out. I made (I'm sure of it) a rookie Core Data mistake in my data model.

I was getting duplicates in the simulator (as well as the phone) because of this mistake:

In the data model I have an entity for medications and all the attributes, blah, blah, blah. I created a class and decided to use just that class to share between 3 table views, current meds, past meds, and allergies. In doing so naturally there was no differentiation. I went back, create a new model, merged yada, yada, and added 3 new entities for the current, past, and allergies. Then set there parent entity to the main med entity and abstracted the parent. When I went through again and reworked the code to the new entities. Everything worked! No more duplications. Well, really, no more triplication.

For the warning I was getting (not error, the app still build, didn't crash and you could still work through it) was do to the managed object context. To silence the infamous +entityForName error I tried something that I either saw in a book or from Apple. I imported the AppDelegate to each of my tables and in fetchedResultsController did this:

- (NSFetchedResultsController *)fetchedResultsController {
MIT2AppDelegate *mit = [[MIT2AppDelegate alloc] init];

if (fetchedResultsController != nil) {
    return fetchedResultsController;
}
    //if (fetchedResultsController == nil) {
    // Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];


    //  the entity name
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Allergies" inManagedObjectContext:mit.managedObjectContext];
[fetchRequest setEntity:entity];

While reworking the code I tried what tomasBULL recommended:
How can I solve NSInternalInconsistencyException', reason: '+entityForName: fail report

That's where the error was coming from. So I went back to what I had before and now, no more error.

I do this in my AppDelegate managedObjectModel

- (NSManagedObjectModel *)managedObjectModel{
    if (managedObjectModel != nil){
        return managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle]  pathForResource:@"MIT2ver 3" ofType:@"mom" inDirectory:@"MIT2ver2.momd"];

        NSURL *momURL = [NSURL fileURLWithPath:path];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return managedObjectModel;
}

Because before I had made another rookie mistake of when I added a model I took out the space. Thanks to udibr answer: Implementation of “Automatic Lightweight Migration” for Core Data (iPhone)

Thanks to everyone who answered. GOD worked it out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文