显示表格视图数据分组在iphone上
问题描述:
我拉从xml数据所以是可能的,以显示他们组?显示表格视图数据分组在iphone上
我现在没有plsit现在我显示数据就像简单,所以现在我想通过什么应该是方法/概念组?
我做了这样的事情,但没有运气
d O I需要做一些排序?像使用NSSortDescriptor ****
如果是的话那么在viewdidload或cellforrowatindxpath??
- (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];
,但我不能按字母顺序排列:(显示它们
}
感谢
答
大部分时间我创建的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];
当然我建议
你避免你每次排序需要一个价值。数据可用时进行一次排序。
有这些东西叫做表格部分。也许你应该看看它们和TableViewSuite示例。 – Nick 2010-09-02 02:17:01
谢谢,但我不知道如何进行,我应该创建词典,然后显示?我应该在哪里添加元素到dic?你能否详细说明 – prajakta 2010-09-02 02:35:05