سؤال

I have been following a few guides and questions on Stack Overflow, but not getting a desired result with basic table view editing.

I have an app which is a table view controller. If I press the plus button, it brings up modally a view controller allowing me to add information to 4 text fields (and one date picker). When I save it, it saves to Core Data and the Table View shows up the information. I want to now simply be able to tap on a cell in the table view, be taken to another view controller and edit the cells if need be. This view controller looks identical to the add view controller.

Currently in the Detail view, it's not showing me the values in the textField and the NSLog comes back NULL.

In the table view, I'm using NSFetchedResultsController to populate the information in the database and the NSManagedContext is as follows from the App Delegate:

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)])
    {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (NSFetchedResultsController *)fetchedResultsController
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

    if (_fetchedResultsController != nil)
    {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Information" inManagedObjectContext:managedObjectContext];
    fetchRequest.entity = entity;
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date.historyDate" ascending:NO];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"date.historyDate" cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;
    return _fetchedResultsController;
}

CellforRow =

cell.textLabel.text = information.vendor;
cell.detailTextLabel.text = information.type; 

My didSelectRowAtIndexPath method is as follows:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{

    DetailViewController *detailViewController = [[DetailViewController alloc] init];
    Information *selectedInformation = [self.fetchedResultsController objectAtIndexPath:indexPath];
    detailViewController.selectedInformation = selectedInformation;     
    [self.navigationController pushViewController:detailViewController animated:YES];
}

The DetailViewController has a property for the Information:

@property (nonatomic, strong) Information *selectedInformation; 

The viewDidLoad in the Detail looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = self.selectedInformation.vendor;
    self.editingNameField.text = self.selectedInformation.vendor;

    NSLog(@"%@", self.editingNameField.text);
}

EDIT: Updated to include prepareForSegue

NSIndexPath *indexPath = [self.timelineTableView indexPathForCell:sender];
Information *selectedInformation = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([segue.identifier isEqualToString:@"Editable Cell"])
{
    DetailViewController *selectedInformationTVC = [segue destinationViewController];

    [selectedInformationTVC setSelectedInformation:selectedInformation];
}

Which calls the setSelectedInformation: - (void)setSelectedInformation:(Information *)selectedInformation { self.title = self.selectedInformation.vendor;

self.editingNameField.text = self.selectedInformation.vendor;
NSLog(@"%@", self.editingNameField.text);

}

I'm not running with both didSelectRow and prepareForSegue, it's one or the other, but both give the same results.

The NSLog is giving me NULL and when the view controller does come on screen, it does not have any information in that field.

I segued to this new view controller in storyboard, so I'm not sure if I should be using prepareForSegue, but that did not work either.

My question is - do I need to do some sort of fetch in the DetailViewController into Core Data?

What do I need to do to get this to work?

Thanks!

هل كانت مفيدة؟

المحلول

Your 2 approaches each have different issues:

When you use didDeselectRowAtIndexPath and viewDidLoad, your problem is that you're creating a view controller that is never placed on screen (because a different one is created by the segue).

When you use prepareForSegue, your problem is that you call setSelectedInformation and it immediately tries to interact with editingNameField and this doesn't exist yet (because the view hasn't been loaded).

So, you should be using prepareForSegue, but you also need to use the viewDidLoad. Set the information to the destination view controller. In the destination, apply that data to the view when it has been loaded.

Finally, from a debugging point of view - log the source data, not just the result. This will lead you towards the source of the issue.

نصائح أخرى

If you're using storyBoards you should definitely make your information transition in the prepareForSegue method since the storyBoard will itself instantiate your view controller.

In your didSelect method :

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Information *information = [self.fetchedResultsController objectAtIndexPath:indexPath];
    // Save your selected information for later
    self.selectedInformation = information;

}

In your prepareForSegue method :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"DetailViewSegue"])
    {
        DetailViewController *dvc = (DetailViewController *)segue.destinationViewController;
        dvc.selectedInformation = self.selectedInformation;
    }
}

That should do it !

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top