0

所以这是基于我的问题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];
}
  1. 想在第一节限制这一点,但不是在第二节

    -(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section {
        return 5;
    }
    
  2. 因此,当我尝试用这种方法填充单元格时,我不确定如何使这种动态变化。我不确定如何确保它选择特定单元格所在部分的“键”(阻塞线):


-(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
    1. H1 内容 1
    2. H1 内容 2
  • 节标题 1
    1. H2含量1
    2. H2含量2
    3. H1 内容 3
4

2 回答 2

1

我将首先创建一个“MyMenuSection”对象,如下所示;

@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 (或您决定初始化/分配数据的任何地方)中,您将执行此操作;

-(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 数据和委托回调将如下所示;

-(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,快乐编码

于 2013-01-24T17:45:17.737 回答
1

听起来你在这里想太多了。我最大的建议是摆脱所有单独的数组并开始将它们嵌套在一起。我的意思是让 menuItems 成为你的数组事件和 myInfo 的 NSArray。

这将使您以后更容易使用 .count 获取动态行数。

例子:

events = [NSArray arrayWithObjects:@"H1 Content 1", @"H1 Content 2", nil];
myInfo = [NSArray arrayWithObjects:@"H2 Content 1", @"H2 Content 2", @"H2 Content 3", nil];

NSArray *menuItems = [NSArray arrayWithObjects:events, myInfo, nil];

现在当你调用数组时

-(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = [NSArray objectAtIndex: section];
    return tempArray.count;
}

至于稍后提取单元名称。

cell.optionName.text = [[menuItem objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
于 2013-01-24T14:51:03.060 回答