分区表视图问题

发布于 2024-11-18 16:00:04 字数 2803 浏览 2 评论 0原文

我正在制作一个数据库应用程序,我可以从 sqlite 数据库中提取结果并将它们放入表中,但是我需要让它们按字母顺序按部分排序。

所以现在我有了这个代码

AArray = [[NSMutableArray alloc] init];
    BArray = [[NSMutableArray alloc] init];
    CArray = [[NSMutableArray alloc] init];
    DArray = [[NSMutableArray alloc] init];
    EArray = [[NSMutableArray alloc] init];
    FArray = [[NSMutableArray alloc] init];
    GArray = [[NSMutableArray alloc] init];
    HArray = [[NSMutableArray alloc] init];
    IArray = [[NSMutableArray alloc] init];
    JArray = [[NSMutableArray alloc] init];
    KArray = [[NSMutableArray alloc] init];
    LArray = [[NSMutableArray alloc] init];
    MArray = [[NSMutableArray alloc] init];
    NArray = [[NSMutableArray alloc] init];
    OArray = [[NSMutableArray alloc] init];
    PArray = [[NSMutableArray alloc] init];
    QArray = [[NSMutableArray alloc] init];
    RArray = [[NSMutableArray alloc] init];
    SArray = [[NSMutableArray alloc] init];
    TArray = [[NSMutableArray alloc] init];
    UArray = [[NSMutableArray alloc] init];
    VArray = [[NSMutableArray alloc] init];
    WArray = [[NSMutableArray alloc] init];
    XArray = [[NSMutableArray alloc] init];
    YArray = [[NSMutableArray alloc] init];
    ZArray = [[NSMutableArray alloc] init];

,并且我有了将每个项目从数据库移动到相关数组的代码,这一切都工作正常。

然后我有这个代码:

All = [NSMutableDictionary dictionaryWithObjectsAndKeys:AArray,@"A",BArray,@"B",CArray,@"C",DArray,@"D",EArray,@"E",FArray,@"F",GArray,@"G",HArray,@"H",IArray,@"I",JArray,@"J",KArray,@"K",LArray,@"L",MArray,@"M",NArray,@"N",OArray,@"O",PArray,@"P",QArray,@"Q",RArray,@"R",SArray,@"S",TArray,@"T",UArray,@"U",VArray,@"V",WArray,@"W",XArray,@"X",YArray,@"Y",ZArray,@"Z",nil];
    AllArray = [NSArray alloc];
    AllArray = [AllArray initWithArray:[[All allKeys]sortedArrayUsingSelector:@selector(compare:)]];

当我运行时它

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [AllArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSArray *Array = [All objectForKey:[AllArray objectAtIndex:section]];
    return [Array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    int sectionindex = section;
    return [AllArray objectAtIndex:sectionindex]; 

}

工作正常,但是如果我上下滚动几次应用程序崩溃而没有任何错误消息。 这与这些部分有关,因为当我添加它们时它崩溃了,但我只是看不出我做错了什么

h 文件中声明的所有内容,我 @property 和 @synthesize 以下

@property (nonatomic,retain) NSMutableDictionary *All;
@property (nonatomic,retain) NSArray *AllArray;
@property (nonatomic, retain) UITableView *TableView;

如果有人可以帮助我,那真的是伟大的!

谢谢!

I have a database app i am making and i can extract the results from a sqlite database and put them in the table, however i need to make them sort alphabetically in sections.

So now i have this code

AArray = [[NSMutableArray alloc] init];
    BArray = [[NSMutableArray alloc] init];
    CArray = [[NSMutableArray alloc] init];
    DArray = [[NSMutableArray alloc] init];
    EArray = [[NSMutableArray alloc] init];
    FArray = [[NSMutableArray alloc] init];
    GArray = [[NSMutableArray alloc] init];
    HArray = [[NSMutableArray alloc] init];
    IArray = [[NSMutableArray alloc] init];
    JArray = [[NSMutableArray alloc] init];
    KArray = [[NSMutableArray alloc] init];
    LArray = [[NSMutableArray alloc] init];
    MArray = [[NSMutableArray alloc] init];
    NArray = [[NSMutableArray alloc] init];
    OArray = [[NSMutableArray alloc] init];
    PArray = [[NSMutableArray alloc] init];
    QArray = [[NSMutableArray alloc] init];
    RArray = [[NSMutableArray alloc] init];
    SArray = [[NSMutableArray alloc] init];
    TArray = [[NSMutableArray alloc] init];
    UArray = [[NSMutableArray alloc] init];
    VArray = [[NSMutableArray alloc] init];
    WArray = [[NSMutableArray alloc] init];
    XArray = [[NSMutableArray alloc] init];
    YArray = [[NSMutableArray alloc] init];
    ZArray = [[NSMutableArray alloc] init];

and i have code to move each item from the database into the relevant array, this all works fine.

I then have this code:

All = [NSMutableDictionary dictionaryWithObjectsAndKeys:AArray,@"A",BArray,@"B",CArray,@"C",DArray,@"D",EArray,@"E",FArray,@"F",GArray,@"G",HArray,@"H",IArray,@"I",JArray,@"J",KArray,@"K",LArray,@"L",MArray,@"M",NArray,@"N",OArray,@"O",PArray,@"P",QArray,@"Q",RArray,@"R",SArray,@"S",TArray,@"T",UArray,@"U",VArray,@"V",WArray,@"W",XArray,@"X",YArray,@"Y",ZArray,@"Z",nil];
    AllArray = [NSArray alloc];
    AllArray = [AllArray initWithArray:[[All allKeys]sortedArrayUsingSelector:@selector(compare:)]];

and this

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [AllArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSArray *Array = [All objectForKey:[AllArray objectAtIndex:section]];
    return [Array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    int sectionindex = section;
    return [AllArray objectAtIndex:sectionindex]; 

}

When i run it works fine, however if i scroll up and down a few times the app crashes without any error message.
It is something to do with the sections as it crashes when i add them in, but i just cannot see what im doing wrong

Everythings declared in the h file and i @property and @synthesize the following

@property (nonatomic,retain) NSMutableDictionary *All;
@property (nonatomic,retain) NSArray *AllArray;
@property (nonatomic, retain) UITableView *TableView;

If anyone could help me it would be really great!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亽野灬性zι浪 2024-11-25 16:00:04

你的字典“全部”似乎没有被保留。您似乎将字典对象直接分配给实例变量,而不是属性(在这种情况下您将使用 self.All)。

如果您的应用程序崩溃且没有任何消息,请确保启用 Xcode 工具栏中的“断点”按钮。这将在调试器中运行应用程序,这将为您提供有关崩溃的更多有用信息。将 NSZombieEnabled 设置为 YES 也有帮助。

Your dictionary "All" does not seem to be retained. You appear to assign the dictionary object directly to the instance variable, not to the property (in which case you would have used self.All).

If your app crashes without a message, then make sure to enable the Breakpoints button in Xcode's toolbar. This will run the app in the debugger, which will give you more helpful information about the crash. Setting NSZombieEnabled to YES also helps.

梦初启 2024-11-25 16:00:04

看一下这里:

最简单的方法是提供排序选择器(有关详细信息,请参阅链接):

sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Take a look here:

The simplest approach is, to provide a sort selector (see the link for details):

sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文