iOS 为啥我的应用程序在重新启动之前不会将数据加载到 tableview 中?

Posted

技术标签:

【中文标题】iOS 为啥我的应用程序在重新启动之前不会将数据加载到 tableview 中?【英文标题】:iOS Why won't my app load data into the tableview until it's restarted?iOS 为什么我的应用程序在重新启动之前不会将数据加载到 tableview 中? 【发布时间】:2014-01-10 19:49:50 【问题描述】:

在我学习如何从 Core Data 正确地将数据加载到分组表视图中的任务中,我构建了一个非常简单的小型双视图应用程序。我使用 MagicalRecord 作为我与 CD 的接口。

当前的问题是,当输入数据按预期保存到我的商店时,表格视图在保存时不会刷新以显示新数据。但是,如果我在模拟器中停止应用程序,然后从 Xcode 重新启动,新数据会显示在 tableview 中。

我正在使用 NotificationCenter 而不是委托进行通信。这是我最初的 View Controller 中的代码(包含并委托给 tableview),然后是 AddViewController 中的代码,用于添加数据。有MagicalRecord经验的人可以照亮我吗?谢谢!

//
//  ViewController.m
//  MRGroupingTest
//
//  Created by Tim Jones on 1/9/14.
//  Copyright (c) 2014 TDJ. All rights reserved.
//

#import "ViewController.h"
#import "ListActivity.h"

@interface ViewController ()


    NSFetchedResultsController *frc;



@end

@implementation ViewController

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self refreshData];
    frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationNewActivityAdded:) name:@"newActivityAdded" object:nil];



- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


-(void) notificationNewActivityAdded:(NSNotification*)notification

    [self.myTableView reloadData];




#pragma mark Table View data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return [[frc sections] count];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    id<NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];
    return rows;



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    id <NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
    return [sectionInfo name];



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    if ([frc sections] > 0)
    
        ListActivity *activity = [frc objectAtIndexPath:indexPath];
        cell.textLabel.text = activity.name;

    

    return cell;



-(void) refreshData


    frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];

    [self.myTableView reloadData];



#pragma mark Table View delegate

@end

这是来自 AddViewController 的代码:

//
//  AddViewController.m
//  MRGroupingTest
//
//  Created by Tim Jones on 1/9/14.
//  Copyright (c) 2014 TDJ. All rights reserved.
//

#import "AddViewController.h"

@interface AddViewController ()

@end

@implementation AddViewController

- (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.


- (IBAction)saveAction:(UIBarButtonItem *)sender

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    ListActivity * thisActivity = [ListActivity MR_createInContext:localContext];
    thisActivity.name =  self.activityField.text;
    thisActivity.category = self.categoryField.text;
    [localContext MR_saveToPersistentStoreAndWait];
    //Inform app
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
    //dismiss view
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];




- (IBAction)cancelAction:(UIBarButtonItem *)sender

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
    //dismiss view
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];




@end

【问题讨论】:

【参考方案1】:

您需要重新加载您的 TableView 数据,即调用reloadData 函数

【讨论】:

感谢您的回复!我显然在这里遗漏了一些关键的东西。我应该在哪里添加这个调用,具体来说? 好吧,他在 refreshData 中有 reloadData 调用。但是很可能某处有一个 nil 指针,或者一个对象的虚假第二个副本,所以 reloadData 不会在真正的 TableView 上被调用。 @HotLicks--您的评论引起了人们的担忧,即我正在使用的样板数据源代码中的局部变量“tableView”与我使用的名为“tableView”的属性之间可能存在冲突视图控制器。所以我回去更改了它,以及反映第一位评论者建议的另外几个更改。查看修改后的 ViewController 代码的编辑。还是不行。 @TimJones - 听起来你已经把自己绑在一个结上了。你需要回去仔细考虑一下,做一些调试(看看你有没有执行过reloadData,执行时指向TableView的指针是否正确)。 这不是我第一次陷入困境。 :) 我会按照你的建议去做并报告。谢谢!

以上是关于iOS 为啥我的应用程序在重新启动之前不会将数据加载到 tableview 中?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的更新后的应用程序在仅适用于 IOS8 的启动时崩溃?

CSHTML 视图在应用程序重新启动之前不会更新

为啥 iOS 在 manifest.json 更改后不会启动 PWA?

Enmap (better-sqlite map) 在应用程序重新启动之前不会更新

我的手机软件有开机自启动的程序都设置好了,为啥开机之后仍然不会自启动

为什么我的Windows服务在重新启动服务之前不会记录