显示表格视图数据分组在iphone上

问题描述:

我拉从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

大部分时间我创建的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