Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Core Data Grouping and Relationships

I'm seeking some assistance with my very first iPhone app. The whole concept of Core Data so far is very overwhelming.


Core Data Model:

Core Data Model

Table View Code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    //return 1;
    return [self.fetchedCoursesArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    return [self.fetchedRoundsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"RoundsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:MyIdentifier];
    }

    Rounds * rounds = [self.fetchedRoundsArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"Date: %@",rounds.date];
    //cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@",courses.city,courses.state];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
    Courses * courses = [self.fetchedCoursesArray objectAtIndex:section];
    return courses.name;
}


Table View Picture (Result of Code):

Table View

The Problem...

The problem is that the grouping is not correct.. it displays the same two items for each group. The rounds need to be grouped according to each course name but I do not know how to accomplish this in code, so I've resorted to asking for help. I've done hours of research now.. lots of Stack Overflow questions, and different tutorials with no avail.

Would anyone be able to provide me with some direction?

Update

Here are some logs of the arrays as they are loaded into the view:

2013-10-23 23:27:16.771 Test 2[1566:70b] COURSES: (null)
2013-10-23 23:27:16.772 Test 2[1566:70b] ROUNDS: (null)
2013-10-23 23:27:16.773 Test 2[1566:70b] COURSES: (null)
2013-10-23 23:27:16.773 Test 2[1566:70b] ROUNDS: (null)
2013-10-23 23:27:16.775 Test 2[1566:70b] COURSES: (
    "<Courses: 0x8ab99a0> (entity: Courses; id: 0x8ab8b40 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Courses/p1> ; data: {\n    city = Arlington;\n    holes = 18;\n    name = \"Jones Park\";\n    rounds = \"<relationship fault: 0x8c35a30 'rounds'>\";\n    state = TX;\n})",
    "<Courses: 0x8ab9c30> (entity: Courses; id: 0x8ab8b50 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Courses/p2> ; data: {\n    city = Arlington;\n    holes = 22;\n    name = \"Test Park\";\n    rounds = \"<relationship fault: 0x8c35b40 'rounds'>\";\n    state = TX;\n})"
)
2013-10-23 23:27:16.776 Test 2[1566:70b] ROUNDS: (
    "<Rounds: 0x8c352a0> (entity: Rounds; id: 0x8c345f0 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Rounds/p1> ; data: <fault>)",
    "<Rounds: 0x8c354c0> (entity: Rounds; id: 0x8c34600 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Rounds/p2> ; data: <fault>)"
)

I see there is a relationship issue.. but I don't think a relationship is necessary to group them in the way I need? Maybe it would make it easier though. Hope this helps..

like image 524
Brandon Avatar asked Oct 21 '22 22:10

Brandon


1 Answers

It looks like you're using the same results whether the section is the first course or the second course. You probably need to drill down your data better.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"RoundsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:MyIdentifier];
    }

    Courses * courses = [self.fetchedCoursesArray objectAtIndex:section];
    Rounds * rounds = [[courses.rounds allObjects] objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"Date: %@",rounds.date];
    return cell;
}

And you'd need to do something similar for numberOfRowsInSection too - just to ensure you're returning the right number of rows. It might be worth looking into NSFetchedResultsController too as it takes a lot of the headache out of dealing with Core Data in UITableView.

like image 182
squarefrog Avatar answered Oct 23 '22 23:10

squarefrog