[英]how to give section alphabetical order in iphone
I have table and one global variable in appdelegate class. 我在appdelegate类中有表和一个全局变量。 I store all date in this global array and showing this array value in controller in table view. 我将所有日期存储在此全局数组中,并在表视图的控制器中显示此数组值。 I have to give section index with alphabetical order by short how to give please help me on this I tried this code but it not working for me Kindly help me. 我必须按字母顺序简短地给段索引赋值,请给我帮助,我尝试了此代码,但它对我不起作用请帮助我。
h.file : h.file :
NSMutableArray *timeZonesArray;
NSMutableArray *sectionsArray;
UILocalizedIndexedCollation *collation;
.mfile .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;
}
}
The perfect example , please refer the code, Sorting Alphabetically 完美的例子,请参考代码, 按字母顺序排序
In order to display/arrange your data in alphabetical which in a array you have to use NSSortDescriptor 为了按字母顺序显示/排列数据,您必须使用NSSortDescriptor在数组中
you have to make the object of this NSSortDescriptor class and give data here which you are fetching from XML 您必须创建此NSSortDescriptor类的对象,并在此处提供要从XML提取的数据
NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:@"itemName" ascending:YES];
Now suppose you have an array sortDescriptors 现在假设您有一个数组sortDescriptors
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];
[yourArray sortUsingDescriptors:sortDescriptors];
I hope this will be usefull to you. 希望对您有用。
If you are looking for section-wise sorting I would hold your various sections in a dictionary that holds arrays for each of the sections and then sort each of those arrays as needed. 如果您要按节进行排序,我会将您的各个节保存在一个字典中,该字典中包含每个节的数组,然后根据需要对每个数组进行排序。 It requires a bit more code on the table delegate methods but allows for complete control over each section's data 它需要在表委托方法上添加更多代码,但可以完全控制每个节的数据
Another option is to encapsulate your data in a different and add a property for the section you want it in and add another sort descriptor to that so it is sorting the array by section and then by any other value. 另一个选择是将数据封装在不同的容器中,并为所需的节添加一个属性,并为其添加另一个排序描述符,以便它按节然后按任何其他值对数组进行排序。 You do that by just adding another NSSortDescriptor to the array in the order you want the array to be sorted by. 您只需按照要对数组进行排序的顺序,将另一个NSSortDescriptor添加到数组即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.