【问题标题】:How to pass the dictionary data to TableView at cellForRowAtIndexPath with dynamic Keys and Values in iOS? [closed]如何在 iOS 中使用动态键和值将字典数据传递给 cellForRowAtIndexPath 的 TableView? [关闭]
【发布时间】:2013-05-16 18:52:20
【问题描述】:
    if ([[[testMenuGroupAry objectAtIndex:0]objectAtIndex:group] isEqualToString:[filteredAry objectAtIndex:objCount]])
    {
        NSLog(@"object count is %d",objCount);
        [groupAry addObject:[itemList objectAtIndex:group]];
    }
}
    [refArray addObject:groupAry];
    [groupAry release];
    [tempDict setObject:[refArray objectAtIndex:objCount] forKey:[filteredAry objectAtIndex:objCount]];

正在将所有数据放入 tempDict,现在我想将 tempDict 传递给 tableview?

【问题讨论】:

  • 您能否提供更多详细信息?您正在尝试在同一视图控制器中传递tempDict
  • 你不想“通过”字典。您想要返回一个单元格、一个 UITableViewCell 的实例或一个子类。该单元格具有表示模型对象属性的子视图,或者在您的情况下,表示字典中的键。但是您的代码必须创建单元格(或重复使用一个单元格)并对其进行配置。
  • @Deepesh 所有功能都在同一个视图控制器中完成。
  • 您需要提供更多详细信息。你想在你的表中显示什么?您想要多个部分,字典中的每个键都为一个部分提供数据吗?
  • 所以,通过它。但是你最好知道一旦你到达那里后如何处理它——没有人愿意为你编写代码。

标签: ios objective-c nsdictionary nsmutabledictionary


【解决方案1】:

你可以这样做。这是用字典填充分区表的一种非常通用的方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sortedKeys = [self.tempDict.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    [self.tableView reloadData];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.tempDict count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.tempDict[self.sortedKeys[section]] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sortedKeys[section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.tempDict[self.sortedKeys[indexPath.section]][indexPath.row];
    return cell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2019-08-06
    • 2021-03-30
    • 2021-12-06
    • 2022-08-09
    • 1970-01-01
    相关资源
    最近更新 更多