【问题标题】:sorting a list of names in Xcode在 Xcode 中对名称列表进行排序
【发布时间】:2015-07-08 08:50:27
【问题描述】:

我有一个表格视图控制器,它可以导入一个带有 NSStrings 的 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

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

还有表格视图:

#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....

任何帮助将不胜感激。

【问题讨论】:

  • 在 NSArray 文档中查找名称中带有“sort”的各种方法。
  • 然后在 NSString 文档中查找名称中带有“compare”的各种方法。
  • 我有,我试过 NSArray *sortedArray = [allTableData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];但我想我错过了什么
  • 你的问题与Xcode无关。

标签: ios objective-c uitableview alphabetical


【解决方案1】:

使用NSSortDescriptor。确保allTableDataNSMutableArray

NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"name"
                                                       ascending:YES];
[allTableData sortUsingDescriptors:@[ desc ]];

然后重新加载tableview。

【讨论】:

    【解决方案2】:

    这可能会对您有所帮助:

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    
        NSArray* sortedArray=[allTableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    

    【讨论】:

    • 在模拟器中它没有给出错误也没有警告,但它仍然没有正确列出
    • 可能你正在寻找这个:stackoverflow.com/questions/805547/…
    • allTableData 必须是 NSMutableArray 吗?
    • 不确定,但我的是 NSMutableArray
    • 我认为这不是我的问题,我认为我不可能将该数组更改为 NSMutableArray。这让我发疯了
    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2011-10-13
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多