Ios 在Xcode中对名称列表进行排序

Ios 在Xcode中对名称列表进行排序,ios,objective-c,uitableview,alphabetical,Ios,Objective C,Uitableview,Alphabetical,我有一个表视图控制器,它导入一个带有NSString的NSObject。在我创建列表的表视图控制器中,不是按字母顺序排列的。 以下是我正在使用的代码片段: 歌曲h: #import <Foundation/Foundation.h> @interface Songs : NSObject @property (nonatomic, retain) NSString* name; @property (nonatomic, retain) NSString* description;

我有一个表视图控制器,它导入一个带有NSString的NSObject。在我创建列表的表视图控制器中,不是按字母顺序排列的。 以下是我正在使用的代码片段:

歌曲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
以及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 "],
清单上还有很多。分段很好,但我不知道如何按字母顺序排列每个分段中的歌曲名称;就像在一个部分,它应该是1-Ahu Gozlum 2-Aglarsa Anam Aglar 3-Aynali Kemer

任何帮助都将不胜感激。

请使用NSSortDescriptor。确保allTableData是NSMutableArray:

然后重新加载tableview。

这可能会帮助您:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

    NSArray* sortedArray=[allTableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

在NSArray文档中查找名称为sort的各种方法。然后在NSString文档中查找名称为compare的各种方法。我已经尝试过NSArray*sortedArray=[allTableData sortedArray Using Selector:@selectorlocalizedCaseInsensitiveCompare:];但是我想我遗漏了一个问题你的问题与Xcode无关。在模拟器中,它没有给出错误或警告,但仍然没有正确列出。可能你在寻找这个问题:allTableData必须是NSMutableArray吗?不确定,但我的是NSMutableArray我想这是我的问题不是,我认为我不可能将该数组更改为NSMUTABLEARRY。它快把我逼疯了
NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"name"
                                                       ascending:YES];
[allTableData sortUsingDescriptors:@[ desc ]];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

    NSArray* sortedArray=[allTableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];