用于按节标题划分的 UI 表视图的 Objective C 动态 NSDictionary [英] Objective C Dynamic NSDictionary for UI Table View divided by Section Headers
问题描述
所以这是基于我在这里的问题
So this is based off of my question here
objective c 何时使用 NSDictionary 而不是 NSArray
对于菜鸟问题深表歉意.我刚开始学习目标 c,我真的很困惑.
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;
}
GAAAHHH,格式正在杀死我.它不会进入代码标记
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"];
cell.optionName.text = [menuItem objectForKey:@"Events"];
所以我想要发生的是:
- 部分标题 1
- H1 内容 1
- H1 内容 2
- H2 内容 1
- H2 内容 2
- H1 内容 3
推荐答案
我会首先创建一个MyMenuSection"对象,如下所示;
I would do this by first creating a "MyMenuSection" object as follows;
@interface MyMenuSection : NSObject {
NSString *title;
NSMutableArray *rows;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSMutableArray *rows;
@end
@implementation
@synthesize title;
@synthesize rows;
@dealloc {
[title release];
[rows release];
[super dealloc];
}
@end
然后在您的 viewDidLoad(或您决定初始化/分配数据的任何地方)中,您将执行此操作;
Then in your viewDidLoad (or wherever you decide to inialize/assign the data) you would do this;
-(void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray menuDetails = [NSMutableArray array];
MyMenuSection section = [[MyMenuSection alloc] init];
section.title = @"Header Section 1 (H1)";
[section.rows addObjects:@"H1 Content 1", @"H1 Content 2", nil];
[menuDetails addObject:section];
[section release];
section = [[MyMenuSection alloc] init];
section.title = @"Header Section 2 (H2)";
[section.rows addObjects:@"H2 Content 1", @"H2 Content 2", @"H2 Content 3", nil];
[menuDetails addObject:section];
[section release];
self.menuDetails = menuDetails;
[menuDetails release];
}
然后你的 UITableView 数据和委托回调看起来像这样;
Then your UITableView data and delegate callbacks would look like this;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [menuDetails count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
} else {
return [[(MyMenuSection *)[menuDetails objectAtIndex:indexPath.section] rows] count];
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [(MyMenuSection *)[menuDetails objectAtIndex:section] title];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *cellIdentifier = @"OptionCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OptionCellIdentifier" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.optionName.text = (NSString *)[[(MyMenuSection *)[menuDetails objectAtIndex:indexPath.section] rows] objectAtIndex:[indexPath.row]];
return cell;
}
YMMV,快乐编码
这篇关于用于按节标题划分的 UI 表视图的 Objective C 动态 NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!