Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将NSFetchedResultsController从UITableViewController迁移到UIViewController_Iphone_Ios_Objective C_Core Data_Nsfetchedresultscontroller - Fatal编程技术网

Iphone 将NSFetchedResultsController从UITableViewController迁移到UIViewController

Iphone 将NSFetchedResultsController从UITableViewController迁移到UIViewController,iphone,ios,objective-c,core-data,nsfetchedresultscontroller,Iphone,Ios,Objective C,Core Data,Nsfetchedresultscontroller,为了避免NSFetchedResultsController固有的一些问题,我决定将UITableViewController更改为UIViewController,只添加一个tableView属性。我不是在使用故事板,而是在以编程的方式制作一切。这样做的目的是使表视图在y轴上从顶部留下大约44 px的空白空间(这样我可以在那里放置一个通用标题)。基本上,我正在尝试将tableview向下移动44像素 问题是,我在fetchedResultsController中遇到断点错误。这是我的密码: V

为了避免NSFetchedResultsController固有的一些问题,我决定将UITableViewController更改为UIViewController,只添加一个tableView属性。我不是在使用故事板,而是在以编程的方式制作一切。这样做的目的是使表视图在y轴上从顶部留下大约44 px的空白空间(这样我可以在那里放置一个通用标题)。基本上,我正在尝试将tableview向下移动44像素

问题是,我在fetchedResultsController中遇到断点错误。这是我的密码:

ViewController.h

@interface ToDoViewController : UIViewController <UITableViewDelegate, SettingsViewControllerDelegate, NSFetchedResultsControllerDelegate, UITableViewDataSource>
@property (nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic) NSFetchedResultsController *fetchedResultsController;
@end

问题是表视图中没有填充nsfetchedresultscontroller应该获取的项。我不知道为什么会这样……可能是因为tableview不是委托或其他什么?

NSFetchedResultsControllerDelegate用于监视对核心数据实体的更改,因此您不需要

    self.tableView = self.fetchedResultsController.delegate;
您需要使用NSFetchedResultsController实现UITableViewDelegate方法。主要是

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
    return sectionInfo.numberOfObjects;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    return cell;
}
-(NSInteger)表格视图:(UITableView*)表格行数节:(NSInteger)节{
id sectionInfo=[self.fetchedResultsController.sections对象索引:section];
返回sectionInfo.numberOfObjects;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject*managedObject=[self.fetchedResultsController对象索引路径:indexPath];
返回单元;
}
注意:创建NSFetchedResultsController后,还必须调用performFetch:


您可以看到一个很好的示例,通过使用主详细信息模板并选中“使用核心数据”来实现所有这些

对于我来说,从UITableViewController到包含UITableView的UIViewController,我必须执行以下操作才能让NSFetchedResultsController正常工作:

  • 使视图控制器处理UITableView和UITableViewDataSource的委托方法:

    @接口UIViewControllerName:UIViewController

  • 将UITableView的委托和数据源(在我的示例中,我使用故事板的outlet将其称为tableView)设置为self(在我的示例中,在viewDidLoad中)


  • 我觉得问题在于,我正在将tableview添加为子视图,而国家自然科学基金委员会不知道如何将其发送到tableview国家自然科学基金委员会不将数据发送到tableview,它只包含数据。tableview应该要求视图控制器通过UITableViewDataSource协议提供数据。如果我尝试创建XIB而不是以编程方式创建tableview,则tableview不会加载。我不确定为什么要在viewDidLoad中输入tableview的设置代码,看看是否有帮助没有问题,我想initWithNibName没有因为某种原因被调用,或者在viewDidLoad之后被调用。
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
        id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
        return sectionInfo.numberOfObjects;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
        return cell;
    }
    
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    appDelegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [appDelegate managedObjectContext];
    
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        // handle error
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1); // fail
    }