I tried to load my Arrays like this, but it loads just first Array and others doesn’t displayed.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"test.plist"];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])
{
NSArray *black = [myDict objectForKey:@"Black"];
self.tableData = [black copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"greenKey"])
{
NSArray *green = [myDict objectForKey:@"Green"];
self.tableData = [green copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"purpleKey"])
{
NSArray *purple = [myDict objectForKey:@"Purple"];
self.tableData = [purple copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"orangeKey"])
{
NSArray *orange = [myDict objectForKey:@"Orange"];
self.tableData = [orange copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"])
{
NSArray *blue = [myDict objectForKey:@"Blue"];
self.tableData = [blue copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"redKey"])
{
NSArray *red = [myDict objectForKey:@"Red"];
self.tableData = [red copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"darkblueKey"])
{
NSArray *darkblue = [myDict objectForKey:@"Darkblue"];
self.tableData = [darkblue copy];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yellowKey"])
{
NSArray *yellow = [myDict objectForKey:@"Yellow"];
self.tableData = [yellow copy];
}
}
-
What I doing wrong?
-
Also, I would like to show Arrays title as Section title, how it can
be done? - And is there a way to limit rows per section (my Array)?
EDIT
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
// Customize the appearance of table view cells.
- (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];
}
NSDictionary *dict = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]];
return cell;
}
EDIT2
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
[sectionsTitle objectForKey:[sectionTitle objectAtIndex:indexPath.section]];
}
Use of undeclared identifier ‘indexPath’; did you mean ‘NSIndexPath’?
Since you are going to divide them to sections, Your tableData should be
NSMutableDictionaryAnd you will also need to hold an array of the keys for the dictionary
Like this
Now in use the
sectionsTitleandtableDatato fill your tableFor any section in the table you can get its title from
[sectionsTitle objectAtIndex:sectionIndex];To get all the elements in a section use
[sectionsTitle objectForKey:[sectionTitle objectAtIndex:sectionIndex]];