1

I am a bit a loss, i have an entity "Contact" which has an attribute "lastName", i would like to organize my contacts into a grouped sectioned tableview but i cannot seem to understand the way to go. Spent the last two days on StackOverflow and i cannot get it to work, i have added "transient" attributes to my entity "Contact", generated the class files, updated the model, changed the code but to no avail, i keep on getting this error message that my transient attribute is not to be found . So back to square one, what gives...? Have read most of that answers here and still does not work....

thank you Mundi....putting this into my contacts.h file gives me a parse error, do i have to @interface it or @implement it ? i don't get it.....found the error for the transient attribute and unNSSortDecriptor (ed) and that fixed that but i just keep on getting all the entries in the section headers and in the table......am at a complete loss

Thank you Ian and Mundi....finally got it to work, worked on a post by gerry 3 dating from 2010...interfaced in the .h file of my entity "Contacts", implemented in the .m file...then changed the fetched results controller in the sectionKeyPath to the NSString that i created in the entity class files...Newbie errors had been committed by myself but perusal of a lot of stack overflow got the day...thanks again

perdido007
  • 33
  • 1
  • 8
  • See my answer on transient attributes in Core Data for the concept: http://stackoverflow.com/questions/25960555/coredata-swift-and-transient-attribute-getters/26614161#26614161 – Ian May 18 '15 at 05:00
  • thanks Ian....will keep you posted.....cheers – perdido007 May 18 '15 at 05:07
  • no not really Ian....your project is in Swift and am in Obj C..thanks all the same.... – perdido007 May 18 '15 at 05:37
  • Your question wasn't tagged. The method is all the same though, you'll just have to look up the translation – Ian May 18 '15 at 13:57

1 Answers1

1

Add a transient property called initial to your Contacts entity. In Contacts.m implement the first letter calculation:

-(NSString *)initial {
   if (self.lastName.length) {
     return [self.lastName subStringToIndex:1];
   }
   return @"";
}

Now all you have to do is set the sectionNameKeyPath of your fetched results controller to initial.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • nice, do you have a similar solution when not using a FetchedResultsController? – DogCoffee May 18 '15 at 10:28
  • If you are using an array, I would recommend an array of `NSDictionary` objects where the key is the initial and the value an array of the person objects. – Mundi May 18 '15 at 11:01
  • I'll keep that in mind. The automatically generated sections in tableviews have be something I've avoided in the past. Will give that a crack! – DogCoffee May 18 '15 at 11:11
  • ok got the parse thing sorted....could you be more specific for your explanation am still at a loss....thanks in advance...if i use your initial in fetched results controller than i get a crash since the lastName attribute can no longer be implemented into the table ... – perdido007 May 18 '15 at 12:27
  • actually this is n to working at all.....i removed your NSString and nothing changed in my build....i still got all of my sections which were the same as my entries....thanks again... – perdido007 May 18 '15 at 12:48
  • @DogCoffee For non-FRC, take a look also at [UILocalizedIndexedCollation](https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalizedIndexedCollation_Class/). Quote: "a convenience for organizing, sorting, and localizing the data for a table view that has a section index. The table view’s data source then uses the collation object to provide the table view with input for section titles and section index titles." – pbasdf May 18 '15 at 23:24
  • @perdido007 Not clear what is your problem. Your reported crashes do not seem to have anything to do with the problem at hand. – Mundi May 18 '15 at 23:28
  • Here is a problem I had - which I could not get any decent answer for. Hopefully either of you guys have a better solution for me. http://stackoverflow.com/questions/29728972/map-object-into-2d-array-swift-for-tableview-sections/29819196#29819196 – DogCoffee May 19 '15 at 03:08