如何在iphone中按字母顺序给出分区
我在appdelegate类中有一个表和一个全局变量。我将所有日期存储在此全局数组中,并在表视图中的控制器中显示此数组值。我必须以简短的字母顺序给出章节索引如何给出请帮助我 我试过这个代码,但它对我不起作用 请帮帮我。如何在iphone中按字母顺序给出分区,iphone,objective-c,Iphone,Objective C,我在appdelegate类中有一个表和一个全局变量。我将所有日期存储在此全局数组中,并在表视图中的控制器中显示此数组值。我必须以简短的字母顺序给出章节索引如何给出请帮助我 我试过这个代码,但它对我不起作用 请帮帮我。 h.file: NSMutableArray *timeZonesArray; NSMutableArray *sectionsArray; UILocalizedIndexedCollation *collation; .mfile #pragma mark -
h.file:
NSMutableArray *timeZonesArray;
NSMutableArray *sectionsArray;
UILocalizedIndexedCollation *collation;
.mfile
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
//return 1;
return [[collation sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//app.networkActivityIndicatorVisible = YES;
NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:section];
return [timeZonesInSection count];
// return app.lstAirports.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:indexPath.section];
a=(airport*)[timeZonesInSection objectAtIndex:indexPath.row];
NSLog(@"str:%@",a);
if(a!=nil){
cell.textLabel.text =a.Name;
cell.detailTextLabel.text=a.Code;
//[app.spinner stopAnimating];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[collation sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [collation sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [collation sectionForSectionIndexTitleAtIndex:index];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
app.originAirport = (airport*)[app.lstAirports objectAtIndex:indexPath.row];
[delegate Originstart:app.originAirport forIndexPath:self.indexPathToChange];
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Set the data array and configure the section data
- (void)setTimeZonesArray:(NSMutableArray *)newDataArray {
if (newDataArray != timeZonesArray) {
[timeZonesArray release];
timeZonesArray = [newDataArray retain];
}
if (timeZonesArray == nil) {
self.sectionsArray = nil;
}
}
为了按字母顺序显示/排列数据,必须使用NSSortDescriptor 您必须创建这个NSSortDescriptor类的对象,并在这里提供从XML获取的数据
NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:@"itemName" ascending:YES];
现在假设您有一个数组sortDescriptors
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];
[yourArray sortUsingDescriptors:sortDescriptors];
我希望这对您有用。如果您正在寻找按节排序,我会将您的各个节保存在一个字典中,该字典保存每个节的数组,然后根据需要对这些数组进行排序。它需要更多的表委托方法代码,但允许对每个部分的数据进行完全控制
另一种方法是将数据封装在不同的文件中,并为所需的节添加一个属性,然后再向该属性添加另一个排序描述符,以便按节对数组排序,然后再按任何其他值对数组排序。您只需按照希望数组排序的顺序向数组中添加另一个NSSortDescriptor即可完成此操作。完美的示例,请参考代码,
ceacare我必须给出如何做到这一点的具体方法,这应该是“使用描述符进行分类”。如果您无法理解描述符,请使用sortedArrayUsingFunction——更多代码,但更直接。