我有一个表视图控制器,它使用NSStrings导入NSObject。在我创建列表的表视图控制器中,不是按字母顺序排列。 这是我正在使用的代码片段:
Songs.h:
#import <Foundation/Foundation.h>
@interface Songs : NSObject
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* description;
@property (assign) int serial;
-(id) initWithName:(NSString*) SongName andDescription:(NSString*)theDescription;
@end
songs.m:
#import "Songs.h"
@implementation Songs
@synthesize name;
@synthesize description;
@synthesize serial;
-(id) initWithName:(NSString *)theName andDescription:(NSString *)theDescription
{
self = [super init];
if(self)
{
self.name = theName;
self.description = theDescription;
}
return self;
}
@end
和tableview:
#import "TableViewController.h"
#import "DetailViewController.h"
#import "Songs.h"
@implementation TableViewController
@synthesize allTableData;
@synthesize filteredTableData;
@synthesize letters;
@synthesize searchBar;
@synthesize isFiltered;
NSArray *SongsIndexTitles;
SongsIndexTitles = @[@"A", @"B", @"C",@"Ç", @"D", @"E", @"F", @"G", @"H", @"I",@"İ", @"J", @"K", @"L", @"M", @"N", @"O", @"Ö", @"P", @"R", @"S",@"Ş", @"T", @"U",@"Ü", @"V", @"Y", @"Z"];
allTableData = [[NSArray alloc] initWithObjects:
[[Songs alloc] initWithName:@"Ağlarsa Anam Ağlar" andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Aynalı Kemer" andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Boşuna " andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Çöpçüler " andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Ahu Gozlum" andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Hepsi Senin mi?" andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Neredesin Sen?" andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Pamuk" andDescription:@"Seslendiren: Abidin"],
[[Songs alloc] initWithName:@"Sen Yoluna Ben Yoluma" andDescription:@"Seslendiren: Abidin "],
[[Songs alloc] initWithName:@"Yolcu Yolunda Gerek" andDescription:@"Seslendiren: Abidin "],
然后列表继续。切片效果很好,但我无法弄清楚如何按字母顺序在每个部分制作歌曲名称;比如在A部分它应该是1-Ahu Gozlum 2-Aglarsa Anam Aglar 3- Aynali Kemer ....
任何帮助将不胜感激。
答案 0 :(得分:2)
使用NSSortDescriptor
。确保allTableData
是NSMutableArray
:
NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES];
[allTableData sortUsingDescriptors:@[ desc ]];
然后重新加载tableview。
答案 1 :(得分:0)
这可能会对您有所帮助:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray* sortedArray=[allTableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];