2013-08-04 43 views
0

我现在有一本字典属性`scoreDictionary构造使得它包含本身是一个嵌套的字典中的值:显示具有相同的密钥中的多个对象表视图

{ 
    Bob = { 
     "2013-08-02 00:27:02 +0000" = 70; 
     "2013-08-02 00:28:17 +0000" = 60; 
    }; 
    Robert = { 
     "2013-08-02 02:53:19 +0000" = 1137; 
    }; 
    Mooga = { 
     "2013-08-02 02:53:04 +0000" = 80; 
    }; 
} 

我能够回到正确的行数对于所需的方法tableView: numberOfRowsInSection:,但在编写tableView: cellForRowAtIndexPath:时遇到困难。具体而言,由于必须有两行行显示相同的密钥“Bob”,但具有不同的得分和日期信息,因此我如何处理indexPath.row,以便它能够在每次调用它时为单元返回不同的信息同样的钥匙?

谢谢!

+0

你不想要部分?你可以让每个名字成为内部字典需要多少行的部分。 – rdelmar

+0

您应该发布更多的代码。向我们展示您为numberOfRowsInSection所做的工作,以及您目前为cellForRowAtIndexPath所做的工作 – jopke

回答

5

目前还不清楚你想要输出的样子。如果你想要的部分,名称是部分标题,你可以像下面这样做。由于你的数据结构,正确的数据看起来很复杂(使用一系列字典会更容易)。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.theData = @{@"Bob":@{@"2013-08-02 00:27:02 +0000":@70, @"2013-08-02 00:28:17 +0000":@60}, @"Robert":@{@"2013-08-02 02:53:19 +0000":@1137}, @"Mooga":@{@"2013-08-02 02:53:04 +0000":@80}}; 
    self.keys = self.theData.allKeys; 
    [self.tableView reloadData]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSLog(@"sections are: %d",self.keys.count); 
    return self.keys.count; 
} 

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = [self.theData[self.keys[indexPath.section]] allKeys][indexPath.row]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[self.theData[self.keys[indexPath.section]] valueForKey:[self.theData[self.keys[indexPath.section]] allKeys][indexPath.row]] ]; 
    return cell; 
} 

这给这个输出(使用权的细节细胞类型):

enter image description here

如果你不想章节标题,你可以删除titleForHeaderInSection方法,并更改的cellForRowAtIndexPath方法如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    NSString *name = self.keys[indexPath.section]; 
    NSString *date = [self.theData[self.keys[indexPath.section]] allKeys][indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",name,date]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[self.theData[self.keys[indexPath.section]] valueForKey:[self.theData[self.keys[indexPath.section]] allKeys][indexPath.row]] ]; 
    return cell; 
} 

唯一的问题是,对于较长的名称,日期和时间字符串太长,切断n按钮值。因此,您需要缩短该字符串,或者转到字幕类型的单元格中,该单元格将在单独的行中。

+0

这是一个绝佳的答案,并感谢您提供屏幕截图!我想知道 - 如果我不想要章节标题,而是只包含所有行的一个部分(并且每行都会显示名称,日期和分数),那么我怎么能够意识到这一点?我的想法是使用“不可见”部分,并在每个单元格中显示部分标题。我想我的后续问题是 - 我怎么能够显示我的单元格中的部分名称,但实际上并没有显示部分标题? – daspianist

+1

@daspianist,我已添加到我的帖子,告诉你如何做到这一点。 – rdelmar

0

只需为字典的每个名称(键)创建一个部分。在每一节中,该行将是输入日期/值。

即使你希望你的行在一起,只是不要实现tableview的任何头,因为这个解决方案/组织与tableView是最适合你的问题。

如果您需要此方法的任何帮助,请告诉我们,以便我们可以显示一些代码来指导您。

0

按照您的示例的NSDictionary(scoreDictionary),在tableview中,以显示根据第2行indexpath“鲍勃”和下段,以显示一个indexpath行“Robert'and‘Mooga’后续样本代码作为波纹管

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView { 

    return [[scoreDictionary allKeys] count]; 
} 

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 

    return [[scoreDictionary valueForKey:[[scoreDictionary allKeys] objectAtIndex:section]]count]; 

} 

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { 

    return [[scoreDictionary allKeys]objectAtIndex:section]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 




    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDictionary *tempScoreDic = [scoreDictionary valueForKey:[[scoreDictionary allKeys]objectAtIndex:indexPath.section] ]; 


    cell.textLabel.text = [NSString stringWithFormat:@"%@",[tempScoreDic valueForKey:[[tempScoreDic allKeys]objectAtIndex:indexPath.row]] ]; 

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[[tempScoreDic allKeys]objectAtIndex:indexPath.row]]; 

    return cell; 




} 
+0

这样做,只是你有(可能)不希望的行为,每次他加载tableView时更改节的顺序,因为字典不保留顺序。例如,他必须在viewDidLoad中按名称对allKeys数组进行排序 –

相关问题