我从xml中提取数据所以可以通过??显示它们分组
我现在没有plsit我正在显示数据同样简单所以现在我想要的是什么应该是方法/概念?
我做了类似的事,但没有运气
d 我需要做一些排序吗?比如使用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];
但我不能按字母顺序显示它们:(
}
谢谢
答案 0 :(得分: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];
当然我建议你每次需要一个值时都要避免排序。在数据可用时进行一次排序。