添加新字符串后对 UITableVIew 进行排序

Posted

技术标签:

【中文标题】添加新字符串后对 UITableVIew 进行排序【英文标题】:Sort UITableVIew after new string added 【发布时间】:2013-11-08 21:57:01 【问题描述】:

当添加新生成的随机字符串时,我需要对我的UITableView 进行排序。

我有这个代码:

- (void)viewDidLoad

    [super viewDidLoad];

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(addRandomString)];

    self.navigationItem.rightBarButtonItem = addButton;

    UIBarButtonItem *sortButton = [[UIBarButtonItem alloc] initWithTitle:@"Sort" style:UIBarButtonItemStyleBordered target:self action:@selector(sortStrings)];

    self.navigationItem.leftBarButtonItem = sortButton;

    stringsArray = [[NSMutableArray alloc]init];
    int string_lenght = 10;
    NSString *symbols = @"ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz";

    for (int y = 0; y<8; y++)
    
        NSMutableString *randomString = [NSMutableString stringWithCapacity:string_lenght];

        for (int i = 0; i<string_lenght; i++)
        
            [randomString appendFormat:@"%C", [symbols characterAtIndex:random()%[symbols length]]];
        
        [stringsArray addObject:randomString];
    
    stringsForTableView = [[NSMutableArray alloc]initWithArray:stringsArray];



- (void) addRandomString 

    int string_lenght = 10;

    NSString *symbols = @"ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz";

    NSMutableString *randomString = [NSMutableString stringWithCapacity:string_lenght];

    for (int i = 0; i<string_lenght; i++)
    
        [randomString appendFormat:@"%C", [symbols characterAtIndex:random()%[symbols length]]];
    

    [stringsForTableView addObject:randomString];

    [self.tableView reloadData];


- (void) sortStrings

    ??????????????????????


感谢您的帮助!

【问题讨论】:

那么你的问题是什么? 我的问题是,我在方法 sortStrings 中需要什么代码?我尝试了很多方法,但它们都无法正常工作。所以我请求您的帮助 【参考方案1】:

对可变数组使用标准排序函数:

[stringsForTableView sortUsingDescriptors:
         @[[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]]];
[self.tableView reloadData];

【讨论】:

以上是关于添加新字符串后对 UITableVIew 进行排序的主要内容,如果未能解决你的问题,请参考以下文章

通过 NSDate 对元素进行排序并添加到 UITableView

将新单元格插入排序的 UITableView 的有效方法?

php 按对象属性对对象数组进行排序。示例:查询后对Wordpress帖子进行排序

group by分组后对组内数据进行排序

将 UITableView 排序为多个部分

如何在数据透视后对python中数据框中的索引列以外的列进行排序