2010-09-30 35 views
0

我有麻煩設置我的cellForRowAtIndexPath與部分。如果我只是顯示所有「曲目」,而不用「主題」分割部分,那麼它可以正常工作。UITableView與部分和解析數據如何創建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 
+0

軌道數據在哪裏?它是在設備上的SQLite數據庫中,它駐留在內存中,還是根據需要從網絡中獲取? – 2010-09-30 17:12:13

+0

@John解析XML。謝謝! – slowman21 2010-09-30 20:08:58

回答

0

我會使用嵌套數組。在的cellForRowAtIndexPath

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]; 

訪問對象:

創建於DataArray

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

你能以某種方式創建章節標題是這樣的:

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

應該明確如何去訪問它們。

+0

感謝您的回覆。嗯...當然,我也想過這個。但是,你將如何做到這一點循環。由於我的數據不是靜態的,並且可以在XML被更改或更新時更改。 – slowman21 2010-10-04 18:08:00

+0

您只需使用NSMutableArray而不是那些NSArrays,循環訪問您的數據並將其添加到正確的位置。你可以使用'[[dataArray objectAtIndex:section] addObject:track]'或類似的東西來添加曲目到你的部分。如果您必須將數據添加到不在dataArray中的部分,則只需添加相應的Section-Array [dataArray addObject:[NSMutableArray array]]; – 2010-10-05 02:24:59