Question

My setup: I have a UIViewController with a TableView inserted in it. Also in the viewController I have a textfield and a slider. I am trying to persist data from my text field, store it and then have fetch request populate the cells with whatever information was placed in the textField. However, when I hit the save button nothing populates the tableCell. The .m file below is of my UIVeiwController. Any help would be great as to why the cell isn't populating. Thanks!

    #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
Was it helpful?

Solution

Nevermind figured it out. I forgot to connect the datasource and delegates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top