iphone - NSFetchRequest 不显示任何内容

标签 iphone ios objective-c uitableview core-data

我试图在 tableView 中显示项目,但我的 fetchResult 数组没有显示任何内容。 我得到这个 NSLog:

2013-09-13 13:02:01.088 Prototype[67018:c07] managed object ()

YPProjectListVC.h

@interface YPProjectListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

YPProjectListVC.m

@interface YPProjectListViewController () {
    SelectionSuccessBlock successBlock;
}
@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * data;
@property (nonatomic, strong) UIRefreshControl *spinner ;
@end

@implementation YPProjectListViewController

@synthesize tableView;
@synthesize data;
@synthesize spinner;
@synthesize managedObjectContext;

这是我的要求:

   - (void)viewDidLoad {
        [super viewDidLoad];
       spinner = [[UIRefreshControl alloc]initWithFrame:CGRectMake(130, 10, 40, 40)];
    
    managedObjectContext = [YPDataSingleton context];
    
    if(managedObjectContext){
        
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
        [request setEntity:entity];
        request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
        
        NSError *error;
        self.data = [[NSMutableArray alloc] init];
        NSArray *fetchResults  = [managedObjectContext executeFetchRequest:request error:&error];
        if (error != nil) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
        data=[fetchResults mutableCopy];
        NSLog(@"managed object %@",fetchResults);
        [self.tableView reloadData];
    } else {
        
        
        [self loadProjectsFromService];
        [spinner addTarget:self action:@selector(loadProjectsFromService) forControlEvents:UIControlEventValueChanged];
        [tableView addSubview:spinner];
        
    }
}

如果 NSManagedObjectContext 为空,这里是我从服务中获取项目的地方。

    -(void)loadProjectsFromService{
         [spinner beginRefreshing];
        self.data = [[NSMutableArray alloc] init];
         [self.tableView reloadData];
        [self.view addSubview:self.tableView];
        __weak typeof(self) weakSelf = self;
        successBlock = ^(NSDictionary *newData) {
            if ([newData count] > 0) {
                [weakSelf refreshData:newData];
            }
           
        };
            [spinner endRefreshing];
        [ypNetManager getProjectListWithSuccessBlock:successBlock error:NULL];
    
    }

pragma mark - 自定义 getter

- (UITableView *)tableView {
    //custom init of the tableview
    if (!tableView) {
        // regular table view
        tableView = [[UITableView alloc] initWithFrame:UIEdgeInsetsInsetRect(self.view.bounds, tableViewInsets) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        return tableView;
    }
    return tableView;
}

#pragma mark - Private methods 

- (void)refreshData:(NSDictionary *)newData {
 
        
        for (NSDictionary *projectDic in newData) {
          [data addObject:[Project createProjectWithDictionary: projectDic inManagedObjectContext:managedObjectContext]];
        }
    
    
        [self.tableView reloadData];
    
}

最佳答案

您应该使用 NSFetchedResultsController,它会为您完成工作,小心地提供和更新您的 TableView 。

看这里:Core Data Tutorial for iOS: How To Use NSFetchedResultsController

编辑

我仍然认为您应该使用上面的方法,但是现在,我可以理解您没有实现 TableView 的委托(delegate)和数据源方法?

基本上你需要实现告诉 TableView 的方法:

数据来源

数据源就是你需要给tableview的,例如:

  1. 它应该有多少个部分
  2. 每个部分有多少行
  3. 要添加到表格单元格的内容是什么

还有更多,但这些是基本方法。

委托(delegate)

当您作为 TableView 的委托(delegate)进行整合时,您只需为 TableView 提供一种通知您以下信息的方式:

  1. 用户点击了其中一个单元格
  2. 用户点击了附件按钮
  3. 用户开始编辑表格

等等……

这里的解释很长,其实很简单,而且有很多教程可以帮助你。

试着看这里:

Apple example project for tableViews

这里

TableView Tutorial

关于iphone - NSFetchRequest 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784883/

相关文章:

android - 如何在 flutter 中创建 JSON 对象?

ios - 连接蓝牙扫描仪后,iOS 中的默认键盘消失

iphone - 境界+快速排序

android - IOS & android 如何获取应用程序的空闲状态?

ios - 从 SDWebImage 中删除图像缓存

php - 从 PHP 到 Objective-C

iphone - 为 UIFont 大小的变化设置动画

ios - 设置 iPhone 模拟器目录和 Xcode 派生数据目录

ios - 具有自定义图像且无边框的 UIBarButtonItem

iphone - 取消时删除 UITableView 单元格的 UIActionSheet 一直显示按下的删除按钮