gpt4 book ai didi

ios - 对 NSDictionary 的 NSArray 进行排序...未完全正常工作

转载 作者:行者123 更新时间:2023-11-29 12:23:41 25 4
gpt4 key购买 nike

所以我有 NSDictionary 项的 NSMutableArray。每个 NSDictionary 项目都有一个键“名称”,应该用于按字母顺序对数组进行排序。除了它的“排序”部分起作用。它没有按准确的字母顺序排序。它会按顺序移动字典中的一些内容,但会乱序留下一些内容。

NSMutableArray *section1 = [champs lastObject];
NSArray *oldSection1 = [NSArray arrayWithArray:section1];
[section1 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dictA = (NSDictionary *)obj1;
NSDictionary *dictB = (NSDictionary *)obj2;

NSString *champName1 = dictA[@"name"];
NSString *champName2 = dictB[@"name"];
return [champName1 compare:champName2];
}];
// Animation
for (NSDictionary *champInfo2 in section1) {
if ([oldSection1 indexOfObject:champInfo2] != [section1 indexOfObject:champInfo2]) {
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:[oldSection1 indexOfObject:champInfo2] inSection:1] toIndexPath:[NSIndexPath indexPathForItem:[section1 indexOfObject:champInfo2] inSection:1]];
}
}

我什至试过这段代码

NSMutableArray *section1 = [champs lastObject];
NSArray *oldSection1 = [NSArray arrayWithArray:section1];
/*[section1 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dictA = (NSDictionary *)obj1;
NSDictionary *dictB = (NSDictionary *)obj2;

NSString *champName1 = dictA[@"name"];
NSString *champName2 = dictB[@"name"];
return [champName1 compare:champName2];
}];*/
section1 = [[NSArray arrayWithArray:section1] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]].mutableCopy;
// Animation
for (NSDictionary *champInfo2 in section1) {
if ([oldSection1 indexOfObject:champInfo2] != [section1 indexOfObject:champInfo2]) {
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:[oldSection1 indexOfObject:champInfo2] inSection:1] toIndexPath:[NSIndexPath indexPathForItem:[section1 indexOfObject:champInfo2] inSection:1]];
}
}

好吧,问题是我没有在重组之前更新我的内容快照,因为我是在视觉上更新它。更新代码:

// Animation
for (NSDictionary *champInfo2 in section) {
if ([oldSection indexOfObject:champInfo2] != [section indexOfObject:champInfo2]) {
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:[oldSection indexOfObject:champInfo2] inSection:[champs indexOfObject:section]] toIndexPath:[NSIndexPath indexPathForItem:[section indexOfObject:champInfo2] inSection:[champs indexOfObject:section]]];
[oldSection removeObject:champInfo2];
[oldSection insertObject:champInfo2 atIndex:[section indexOfObject:champInfo2]];
}
}

最佳答案

您代码的上半部分(即排序)工作正常。如果使用排序描述符可以简化它,但这绝对不是问题。

真正的问题在于移动事物的代码。

在第一次移动之前 oldSection1 的索引和 self.collectionView是同步的,即如果一个项目 XYZ在索引 ioldSection1 , 那么它也在索引 iself.collectionView .

然而,在第一次移动之后,索引变得不同步,因为移动仅在 self.collectionView 中执行。 , 但不在 oldSection1 中.例如,如果您以

开头
oldSection1    = C D A B
collectionView = C D A B

你希望它成为

sorted =         A B C D

C的第一步之后数据看起来像

oldSection1    = C D A B
collectionView = A C D B

什么时候搬家D到它的新地点,C反而被移动了。

要解决此问题,请制作 oldSection1可变的,并在 self.collectionView 中移动时执行“平行”移动.

关于ios - 对 NSDictionary 的 NSArray 进行排序...未完全正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29870578/

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