1

I am trying to display items in a tableView, but my fetchResult Array does not show anything. I get this 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;

Here is my request:

   - (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];
        
    }
}

Here is where I get my Projects from the services if NSManagedObjectContext is empty.

    -(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 - Custom 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];
    
}
4

1 回答 1

1

您应该使用 NSFetchedResultsController,它会为您完成工作,仔细地提供和更新您的表格视图。

看这里:iOS 核心数据教程:如何使用 NSFetchedResultsController

编辑

我仍然认为你应该使用上面,但现在,我可以理解你没有实现表视图的委托和数据源方法?

基本上你需要隐含告诉表格视图的方法:

数据源

数据源是你需要给表格视图的,例如:

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

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

代表

当您符合表格视图的委托时,您只需为表格视图提供一种通知您以下内容的方法:

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

等等...

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

试试看这里:

Apple 的 tableViews 示例项目

和这里

表视图教程

于 2013-09-13T11:13:34.660 回答