So this is based off of my question here
objective c when to use NSDictionary instead of NSArray
Apologies for the noob questions. I just started learning objective c and I’m really confused.
- (void)viewDidLoad
{
[super viewDidLoad];
headers = [NSArray arrayWithObjects:@"Header Section 1 (H1)", @"Header Section 2 (H2)", nil];
events = [NSArray arrayWithObjects:@"H1 Content 1", @"H1 Content 2", nil];
myInfo = [NSArray arrayWithObjects:@"H2 Content 1", @"H2 Content 2", @"H2 Content 3", nil];
menuDetails = [[NSMutableDictionary alloc] init];
for(NSString *event in events){
[menuDetails setValue:event forKey:@"Header Section 1 (H1)"];
}
for(NSString *info in myInfo){
[menuDetails setValue:info forKey:@"Header Section 1 (H2)"];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [[menuDetails allKeys] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[[menuDetails allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:section];
}
-
Wanted to limit this for the First Section but not on the 2nd section
-(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section { return 5; } -
So I’m not really sure how to make this dynamic when I try to fill up the cells with this method. I’m not really sure how to make sure it selects the “Key” of the section the specific cell is at (blocked line) :
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *CellIdentifier = @"OptionCellIdentifier";
MenuOptionTableCell *cell = (MenuOptionTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OptionCellIdentifier" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *menuItem = [[menuDetails valueForKey:[[[menuDetails allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.optionName.text = [menuItem objectForKey:@"Events"];
return cell;
}
EDIT: GAAAHHH, the formatting is killing me. It won’t get inside the code markup
This is where Idk what to do:
cell.optionName.text = [menuItem objectForKey:@”Events”];
So what I wanted to happen is:
- Section Header 1
- H1 Content 1
- H1 Content 2
- Section Header 1
- H2 Content 1
- H2 Content 2
- H1 Content 3
I would do this by first creating a “MyMenuSection” object as follows;
Then in your viewDidLoad (or wherever you decide to inialize/assign the data) you would do this;
Then your UITableView data and delegate callbacks would look like this;
YMMV, Happy Coding