Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2010, 05:15 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 77
tazboy is on a distinguished road
Default Core Data NSSet to fill tableview

Basically I have an NSSet of students and I want their names property to populate a tableview. More info below.

I have a course object that I've passed through a few view controllers. This course object has properties of name, hour and an NSSet of students, whom all have a name property. For some reason I can't figure out how to populate a tableview with that set of students' names.

I've tried studentArray = [[course students] allObjects]; but that doesn't seem to work. I just really need to order the student names and then put them in a tableview. I just can't figure this out.

Any ideas?

Thanks.

Last edited by tazboy; 07-10-2010 at 05:29 PM.
tazboy is offline   Reply With Quote
Old 07-10-2010, 05:42 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 30
raphaelharboun is on a distinguished road
Default

if you used a NSFetchRequest to get your course object, it may have lost the reference to your object the time you use it in your view controller.
have you done a copy of this object after the request ? you should do.

Just put a breakpoint to see if your object is still filled with studends...

With it's right, fill an array with the students and sort it with a NSSortDescriptor.
NSArray *a= [course.students allObjects]
NSSortDescriptor *d = [[NSSortDesciptor alloc] initWithKey:@"name"];
a = [a sortedArrayUsingDescriptor:[NSArray arrayWithObject:d]];
[d release];

i wrote the code directly, the syntax may not be all right..
just try.
but start by checking with the debbuger the value of your course object.
raphaelharboun is offline   Reply With Quote
Old 07-10-2010, 05:49 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 77
tazboy is on a distinguished road
Default

They are still there because when I put this code in viewDidLoad I get everything I expect.

Code:
NSLog(@"Course Hour: %@", course.hour);
NSLog(@"Course Name: %@", course.name);
NSLog(@"Students are: ");
for (Student *aStudent in course.students) 
      NSLog(aStudent.name);
tazboy is offline   Reply With Quote
Old 07-10-2010, 06:20 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 30
raphaelharboun is on a distinguished road
Default

So have you tried the code i gave you ?

Could you give the entire code of your view controller, the problem may come from an other method.
raphaelharboun is offline   Reply With Quote
Old 07-10-2010, 06:40 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 77
tazboy is on a distinguished road
Default

I tried your code. Here is the working code.

Code:
NSArray *a= [course.students allObjects];
NSSortDescriptor *d = [[NSSortDescriptor alloc] initWithKey:@"name" 
    ascending:YES];
a = [a sortedArrayUsingDescriptors:[NSArray arrayWithObject:d]];
[d release];
I believe the issue with your code is that it doesn't get the names of the students. NSArray *a seems to be holding an array of student objects.

Here is the output when I do
NSLog(@"Students' names are: %@", [a componentsJoinedByString:@", "]);

Code:
2010-07-10 18:31:38.572 PickEm[7715:20b] Students' names are: <Student: 0xf5a120> (entity: Student; id: 0xf59f60 <x-coredata://E4C16B60-53CA-46C2-990E-0AF6151DAA34/Student/p2> ; data: {
    course = 0xf3a7e0 <x-coredata://E4C16B60-53CA-46C2-990E-0AF6151DAA34/Course/p1>;
    name = Billy;
    weight = 2;
}), <Student: 0xf5a1a0> (entity: Student; id: 0xf4d520 <x-coredata://E4C16B60-53CA-46C2-990E-0AF6151DAA34/Student/p4> ; data: {
    course = 0xf3a7e0 <x-coredata://E4C16B60-53CA-46C2-990E-0AF6151DAA34/Course/p1>;
    name = George;
    weight = 2;
For now, all I need to do is the tableView:cellForRowAtIndexPath: method and set the cell.textLabel.text to the names of the students.
tazboy is offline   Reply With Quote
Old 07-11-2010, 11:21 AM   #6 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 30
raphaelharboun is on a distinguished road
Default

lol, it looks fine to me
The NSArray now contains all the student objects you required, you just have to get the property you want => the name.
now you can set the cell atributes with the content of the object.
cell.textLabel.text = [[a objectAtIndex:indexPath.row] name];

If you want to make a core data request which give you only one property of an object, you should check the NSExpression class.

But with what i just gave you, it should works fine.
raphaelharboun is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 326
15 members and 311 guests
akphyo, alexP, cgokey, EXOPTENDAELAX, flamingliquid, guusleijsten, mariano_donati, ohmniac, Paul Slocum, PavelSea, SLIC, Sloshmonster, Sonuye857, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:33 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0