Reputation: 51
What is the best way to create a plist to store words and their definition, to then use to populate a table view divided alphabetically into sections, with each cell leading to a detail view. I have my table and plist connected and understand how to get detail view with prepare for segue. My issues are with plist layout and then using that for section head titles and getting words to correspond to correct letter.
The last two days have been spent with multiple searches on google exceeding 30 pages. I've looked at terms like predicate but I'm confused.
I'm not expecting someone to code it up but a list of steps would be a lifesaver. I fear failing to section the table would result in bad used experience.
Upvotes: 1
Views: 97
Reputation: 9589
Just follow the below this.
1.Create the PList and give the relevant name
2.Do the following things
Key Type Value
Root Array (2 items)
->Expend the arrow of root (to below-down) direction
Item 0 Dictionary (2 items)
->Expend the arrow of Item 0 (to below-down) direction->Click + and add the below things
Name String First Section Name
List Array (1 items)
->Expend the arrow of List (to below-down) direction->Just Click + for adding item 0
Item 0 Dictionary (6 items)
->Expend the arrow of Item 0 (to below-down) direction ->Click + and the below things
Name String EnterName
Value String
Key String Name
UIType String TextField
InputType String Text
Mandatory Boolean YES
Then if you click the root there is +.Just click the + and you can see the Item 1
Key Type Value
Root Array (2 items)
Item 0 Dictionary (2 items)
Item 1 Dictionary (2 items)
->Expend the arrow of root (to below-down) direction
Item 1 Dictionary (2 items)
->Expend the arrow of Item 1 (to below-down) direction-Click + and add the below things
Name String Second Section Name
List Array (1 items)
->Expend the arrow of List (to below-down) direction-Just Click + for adding item 0
Item 0 Dictionary (6 items)
->Expend the arrow of Item 0 (to below-down) direction-Click + and the below things
Name String Enter Age
Value String
Key String Age
UIType String TextField
InputType String Text
Mandatory Boolean YES
3.Then Create NSObject Class name as PList.
in .h
#import <Foundation/Foundation.h>
@interface PList : NSObject
+ (NSMutableArray *)arrayPlistInit:(NSString *)plistName;
@end
in .m
+(NSMutableArray *)arrayPlistInit:(NSString *)plistName
{
NSString *stringPath = [[NSBundle mainBundle]pathForResource:plistName ofType:@"plist"];
NSMutableArray *arrayPlist = [NSMutableArray arrayWithContentsOfFile:stringPath];
return arrayPlist;
}
4.in viewDidLoad
arrayTable = [PList arrayPlistInit:stringPlistName];
5.Then viewForHeaderInSection
1.Create the Custom Label and View(see the below the Coding in viewForHeaderInSection)
UILabel *labelSection;
UIView *viewSection = [[UIView alloc]init];
viewSection.frame = CGRectMake(0, 0, tableviewCreate.frame.size.width, 20);
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableviewCreate.frame.size.width, 20);
[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = [[arrayTable objectAtIndex:section] objectForKey:@"Name"];
labelSection.text = name;
Upvotes: 1
Reputation: 12254
It's hard to answer this question with such vague details, but I will try. Particularly not knowing how you are storing those words in the plist.
But assuming you are simply storing strings in that plist, then just read the entire file, then grab the first letter of all the words and put them in a NSSet (a set is a data structure that only allows any data to appear once). With that you will have your index that can be used to create the sections.
Then map your NSSet to a dictionary. Each key will be one of the values in the set, and their values will arrays containing the words that begin with that letter.
I have not implemented this, but I'd follow this approach.
Upvotes: 0