gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:52:31 24 4
gpt4 key购买 nike

添加新生成的随机字符串时,我需要对 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{

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

}

感谢您的帮助!

最佳答案

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

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

关于ios - 添加新字符串后对 UITableVIew 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com