2010-09-02 127 views
0

我拉從xml數據所以是可能的,以顯示他們組?顯示錶格視圖數據分組在iphone上

我現在沒有plsit現在我顯示數據就像簡單,所以現在我想通過什麼應該是方法/概念組?

我做了這樣的事情,但沒有運氣

d O I需要做一些排序?像使用NSSortDescriptor ****

如果是的話那麼在viewdidloadcellforrowatindxpath?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //UITableView *tableView; 
    UITableViewCell *cell; 
    static NSString *CellIdentifier = @"Cell"; 


    **Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];** 

///如何將它們排列在組中?

我想這

NSString *keys =aBook.name; 
NSMutableDictionary *dicta = [NSMutableDictionary dictionary]; 
    [dicta setObject:keys forKey:@"message"]; 


    NSSortDescriptor *sortNameDescriptor = 
    [[[NSSortDescriptor alloc] 
     initWithKey:@"message" ascending:YES] 
    autorelease]; 

,但我不能按字母順序排列:(顯示它們

} 

感謝

+0

有這些東西叫做表格部分。也許你應該看看它們和TableViewSuite示例。 – Nick 2010-09-02 02:17:01

+0

謝謝,但我不知道如何進行,我應該創建詞典,然後顯示?我應該在哪裏添加元素到dic?你能否詳細說明 – prajakta 2010-09-02 02:35:05

回答

0

大部分時間我創建的NSDictionary,其中每個項目有一個鍵包含sectionName並且該值是用於填充單元格的任何內容的列表。如果有:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return [[data allkeys] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSString* key = [[data allkeys] objectAtIndex:section]; 
    return [[data objectForKey:key] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [[data allkeys] objectAtIndex:section]; //If key is NSString* of course 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ...//classical code the retrieve a valid cell 
    NSString* key = [[data allKeys] objectAtIndex:indexPath.section]; 
    NSObject* obj = [[data objectForKey:key] objectAtIndex:indexPath.row]; 
    ...//Setup the cell with obj properties. 
} 

您可以優化該代碼很多,但對於少量數據,它可以正常工作。

,如果你想要的任何集團或價值進行排序,你可以使用NSSortDescriptors像這樣爲例(排序字符串數組)

NSSortDescriptor* sortAsc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES]; 
[anyArrayOfNSString sortUsingDescriptors:[NSArray arrayWithObject:sortAsc]];   
[sortAsc release]; 
當然我建議

你避免你每次排序需要一個價值。數據可用時進行一次排序。

+0

你的意思是這種方式我可以按字母順序排列它們? – prajakta 2010-09-02 07:07:09

+0

@arutema:見上面的修改後。 – VdesmedT 2010-09-02 13:45:18

+0

ok 1懷疑..什麼是issueNumbers? (一個數組)和initWithKey:應該是零或我的密鑰?看到我上面修改的代碼 – prajakta 2010-09-03 01:24:51