简体   繁体   English

如何按字母顺序对tableview单元格进行排序

[英]how to sort a tableview cell alphabetically

i have date view cell that is been filled by an array, i need to add two buttons one for A to Z and other Z to A. I able to sort the array but not sure how to change it in runtime i mean in the table view pressing the buttons. 我有一个由数组填充的日期视图单元格,我需要添加两个按钮,一个用于A到Z,另一个用于Z到A。我能够对数组进行排序,但是不确定如何在运行时更改它,我的意思是在表中查看按下按钮。 This is te code i using to sort: 这是我用来排序的代码:

[filePathsArray sortUsingSelector:@selector(compare:)];

The table view is just regular table view been filed with an array from a csv file. 表格视图只是常规表格视图,是使用csv文件中的数组归档的。 This is how i get the array paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 这是我如何获取数组路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];
NSMutableArray*   fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
//--- Listing file by name sort
NSLog(@"\n File list %@",fileList);

//---- Sorting files by extension
NSMutableArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];

[filePathsArray sortUsingSelector:@selector(compare:)];
NSLog(@"\n sort by");

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.csv'"];
filePathsArray =  [filePathsArray filteredArrayUsingPredicate:predicate];
NSLog(@"\n\n Sorted files by extension %@",filePathsArray);

any help or orientation would be appreciated. 任何帮助或方向将不胜感激。 Thanks. 谢谢。

Use sortUsingComparator instead of sortUsingSelector to implement the different sorting directions: 使用sortUsingComparator而不是sortUsingSelector来实现不同的排序方向:

[filePathsArray sortUsingComparator:^NSComparisonResult(NSString* fp1, NSString* fp2) {
  return [fp1 compare:fp2];
}];

and to revert the sort direction, simply swap the parameters for compare : 并返回排序方向,只需交换参数进行compare

[filePathsArray sortUsingComparator:^NSComparisonResult(NSString* fp1, NSString* fp2) {
  return [fp2 compare:fp1];
}];

Your AZ and ZA buttons can then simply sort using one of those methods and call [tableView reloadData] . 然后,您的AZZA按钮可以简单地使用这些方法之一进行排序,然后调用[tableView reloadData]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM