【问题标题】:UITableView with Sections and Parsed Data how to create cellForRowAtIndexPathUITableView 与 Sections 和 Parsed Data 如何创建 cellForRowAtIndexPath
【发布时间】:2010-09-30 16:05:54
【问题描述】:

我在使用 Sections 设置 cellForRowAtIndexPath 时遇到问题。如果我只显示所有“曲目”而不按“主题”分隔部分,那么它可以正常工作。

Track *aTrack = [appDelegate.tracks objectAtIndex:row];
cell.textLabel.text = aTrack.track_title;

我已经设置了我的 numberOfSectionsInTableView、numberOfRowsInSection、titleForHeaderInSection。这些都很好。但我真的不知道如何设置 cellForRowAtIndexPath。

在研究了更多之后,最好的方法似乎是使用 NSDictionary 来显示我的单元格...

NSDictionary *dictionary = [sectionTopicArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Tracks"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

有人可以给我一些关于如何制作一个可以保存我所有字典的 MutableArray(sectionTopicArray) 的指示吗?我可以在脑海中看到以下内容,但只是不知道如何用代码表达它。

sectionTopicArray
[0] Dictionary 1 with key Tracks
    Track1
    Track2
    Track3
[1] Dictionary 2 with key Tracks
    Track4
[2] Dictionary 3 with key Tracks
    Track5
    Track6

basically I'm thinking...
Section0 = Dictionary1 = Topic1 
Section1 = Dictionary2 = Topic2 
Section2 = Dictionary3 = Topic3

【问题讨论】:

  • 轨道数据在哪里?它是在设备上的 SQLite DB 中,是驻留在内存中,还是根据需要从网上获取?
  • @John 解析了它的 XML。谢谢!

标签: iphone xcode uitableview nsxmlparser


【解决方案1】:

我会使用嵌套数组。

创建数据数组:

    NSArray *dataArray = [NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:    // Section 0
                       track1,                      //  Item 0
                       track2,                      //  Item 1
                       track3,                      //  Item 2  (indexpath 0.2)
                       nil],
                      [NSArray arrayWithObjects:    // Section 1
                       track4,                      //  Item 0  (indexpath 1.0)
                       nil],
                      [NSArray arrayWithObjects:    // Section 2
                       track5,                      // Item 0
                       track6,                      // Item 1   (indexpath 2.1)
                       nil],
                      nil];

cellForRowAtIndexPath 中的访问对象:

Track *trackAtIndexPath = [[dataArray objectAtIndex:indexpath.section] objectAtIndex:indexpath.row];

您可以像这样创建章节标题:

    NSArray *sectionTitles = [NSArray arrayWithObjects:
                          @"Section 0",
                          @"Section 1",
                          @"Section 2",
                          nil];

应该清楚如何访问它们。

【讨论】:

  • 感谢您的回复。嗯……当然,我也想过。但是你将如何在一个循环中做到这一点。因为我的数据不是静态的,如果 XML 被更改或更新,可能会发生变化。
  • 您只需使用 NSMutableArray 而不是那些 NSArray,循环遍历您的数据并将其添加到正确的位置。您可以使用[[dataArray objectAtIndex:section] addObject:track] 或类似的东西将曲目添加到您的部分。如果您必须将数据添加到 dataArray 中尚不存在的部分,则只需添加相应的 Section-Array [dataArray addObject:[NSMutableArray array]];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多