gpt4 book ai didi

ios - 获取排序数组中的 IndexPath

转载 作者:行者123 更新时间:2023-11-29 11:00:20 26 4
gpt4 key购买 nike

我正在获取从 plist 中排序的值,并将它们显示在 tableview 中。我提供了输入将写入 plist 的自定义类别的功能。但我希望按排序的字母顺序插入。有人可以建议我如何获取必须插入的行的 indexpath 吗?我使用了以下代码,其中自定义条目将插入到 0 位置

NSInteger section = 0;
NSInteger row = 0;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

NSDictionary *categoryDictionary = [NSMutableDictionary dictionary];
[categoryDictionary setValue:newCategoryName forKey:@"name" ];
[categoryDictionary setValue:@"custom.png" forKey:@"image"];

[[self categoriesArray]insertObject:categoryDictionary atIndex:row];
[[self categoriesArray] writeToFile:[self dataFilePath] atomically:YES];

NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath];
[[self tableView]insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];

最佳答案

尝试

NSMutableDictionary *categoryDictionary = [NSMutableDictionary dictionary];
[categoryDictionary setObject:newCategoryName forKey:@"name" ];
[categoryDictionary setObject:@"custom.png" forKey:@"image"];

//Add new category to array
[self.categoriesArray addObject:categoryDictionary];

//Sort Array
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[self.categoriesArray sortUsingDescriptors:@[sortDescriptor]];

//Write array to plist
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];

NSInteger section = 0;
//Get the index of saved Category item
NSInteger row = [self.categoriesArray indexOfObject:categoryDictionary];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

[self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationRight];

关于ios - 获取排序数组中的 IndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170805/

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