所以这是基于我的问题here
目标 c 何时使用 NSDictionary 而不是 NSArray
为菜鸟问题道歉。我刚开始学习目标c,我真的很困惑。
- (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];
}
想在第一节限制这一点,但不是在第二节
-(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section { return 5; }
因此,当我尝试用这种方法填充单元格时,我不确定如何使这种动态变化。我不确定如何确保它选择特定单元格所在部分的“键”(阻塞线):
-(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;
}
编辑: GAAAHHH,格式正在杀死我。它不会进入代码标记
这就是 Idk 要做的事情:
cell.optionName.text = [menuItem objectForKey:@"Events"];
所以我想要发生的是:
- 节标题 1
- H1 内容 1
- H1 内容 2
- 节标题 1
- H2含量1
- H2含量2
- H1 内容 3