TableView单元格不显示信息

时间:2014-05-07 17:25:28

标签: ios uitableview core-data

我的设置: 我有一个插入了TableView的UIViewController。同样在viewController中我有一个文本字段和一个滑块。我试图从我的文本字段中保留数据,存储它然后让获取请求使用textField中放置的任何信息填充单元格。但是,当我点击保存按钮时,没有任何内容填充tableCell。下面的.m文件是我的UIVeiwController。任何帮助都很好,为什么细胞不会填充。谢谢!

    #import "BottleViewController.h"
    #import "LogEntry.h"
    #import "CoreDataStack.h"


    @interface BottleViewController () <NSFetchedResultsControllerDelegate>

    @property (strong, nonatomic) IBOutlet UILabel *ouncesLabel;
    @property (strong, nonatomic) UISlider *slider;
    @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    @property (strong, nonatomic) IBOutlet UILabel *entryAmount;
    @property (strong, nonatomic) IBOutlet UITextField *textField;

    @end

    @implementation BottleViewController



    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [self.fetchedResultsController performFetch:nil];
    }

    -(void)dismissSelf{

        [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    }

    -(void)insertLogEntry {
        CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
        LogEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"LogEntry" inManagedObjectContext:coreDataStack.managedObjectContext];
        entry.amount = self.slider.value;
        entry.date = [[NSDate date] timeIntervalSince1970];
        entry.testString = self.textField.text;

        [coreDataStack saveContext];

    }

    - (IBAction)backWasPressed:(id)sender {
        [self dismissSelf];
    }






    - (IBAction)mySlider:(id)sender {
        self.slider = (UISlider*)sender;
        NSString *newText = [[NSString alloc] initWithFormat:@"%d", (int)self.slider.value];

        self.ouncesLabel.text = newText;

    }
    - (IBAction)saveWasPressed:(id)sender {
        [self.view endEditing:YES];
        [self insertLogEntry];
    }

    -(NSFetchRequest *)entryListFetchRequest{
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"LogEntry"];
        fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];


        return fetchRequest;
    }

    -(NSFetchedResultsController *)fetchedResultsController{
        if(_fetchedResultsController != nil){
            return _fetchedResultsController;
        }

        CoreDataStack *coreDataStack =[CoreDataStack defaultStack];
        NSFetchRequest *fetchRequest = [self entryListFetchRequest];

        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
        _fetchedResultsController.delegate = self;

        return _fetchedResultsController;

    }

    #pragma mark UITableViewDelegates

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

        return self.fetchedResultsController.sections.count;

    }

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

        id<NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
        return [sectionInfo numberOfObjects];

    }

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

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

        LogEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];


        cell.textLabel.text = entry.testString;


        return cell;
    }

    -(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
        [self.tableView reloadData];
    }


    @end

1 个答案:

答案 0 :(得分:0)

没关系弄明白了。我忘了连接数据源和代理。