Search code examples
ioscore-datatableviewcellrelation

tableView show title use core data (relation)


I have 2 entities (and two tableViews) my first entity name is Person second is Event, in my first tableView there names of persons, at the second their events, I have relation one to many between Person and Event. I just want that when user tap on one of the names the second tableView will show him that person's events (type). the problem is that I get empty cells.

this is how I add new event:

 - (void) addEventControllerDidSave{


        NSManagedObjectContext* context = [self managedObjectContext];
        Event *newEvent = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
        [newEvent setType:@"dennis"];
        [currentPerson addEventsObject:newEvent];

        NSError *error;
        if (![[self managedObjectContext] save:&error])
        {
            NSLog(@"Problem saving: %@", [error localizedDescription]);
        }

        [self dismissViewControllerAnimated:YES completion:nil];
    }

I know that this method is not dynamic and I will get always event type "dennis", this just for the test.

my second tableView methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[self currentPerson] events] count];
}

most important method I think the problem is here (or in the save method):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"eventsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (nil == cell)
    {
        NSEnumerator *enumerator = [[[self currentPerson] events] objectEnumerator];
        Event *event = [[enumerator nextObject]objectAtIndexPath:indexPath];
        [[cell textLabel]setText:[event type]];
    }

    return cell;
}

update: as ophychius said I move my define cell lines out of the if and change them to:

//create cell use relation
    NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects];
    Event *newEvent = [eventsArray objectAtIndex:indexPath.row];
    [[cell textLabel]setText: [newEvent type]];

Solution

  • Try moving the lines

        NSEnumerator *enumerator = [[[self currentPerson] events] objectEnumerator];
        Event *event = [[enumerator nextObject]objectAtIndexPath:indexPath];
        [[cell textLabel]setText:[event type]];
    

    outside of the if block. Inside the if needs to be your code that creates the cell if you dont have one yet (that you try to retrieve just before the if block)

    Right now you are probably not creating cells, and if you are, you are not putting text in them because once you have a cell, the if block is skipped.

    Here is an example:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        NSEnumerator *enumerator = [[[self currentPerson] events] objectEnumerator];
        Event *event = [[enumerator nextObject]objectAtIndexPath:indexPath];
        [[cell textLabel]setText:[event type]];
    
        return cell;
    }