xcode iOS 反转 tableView 排序顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11264066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:44:31  来源:igfitidea点击:

iOS Reverse tableView sort order

iphoneiosxcodesortingtableview

提问by Lucas Vallim da Costa

Is there a way to reverse the tableView order?

有没有办法反转tableView的顺序?

For example, I have a tableView that sorts by firstUpdated to LastUpdated. It does automatically cause its composed by plist data. But what if I want to put the newest data on top and the older on bottom?

例如,我有一个按 firstUpdated 到 LastUpdated 排序的 tableView。它会自动导致其由 plist 数据组成。但是如果我想把最新的数据放在顶部,旧的放在底部呢?

回答by Dima

The other solution will work fine but this one is a bit shorter.

另一种解决方案可以正常工作,但这个解决方案要短一些。

NSArray *reversedArray = [[originalArray reverseObjectEnumerator] allObjects];

NSArray *reversedArray = [[originalArray reverseObjectEnumerator] allObjects];

回答by Raymond Wang

You should sort the array you use to populate the table view.

您应该对用于填充表视图的数组进行排序。

The other two answers would work, but I have another way of doing this if you need to sort by any property of the object:

其他两个答案可行,但如果您需要按对象的任何属性进行排序,我还有另一种方法:

You can create a method -sortMyArray

你可以创建一个方法 -sortMyArray

and it will look like:

它看起来像:

-(void)sortMyArray
{
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"SomeObjectSortProperty" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    [MyArray sortUsingDescriptors:sortDescriptors];
    [sortDescriptor release];
}

and of course, every time after you call this method, you need to call your table view reload data method.

当然,每次调用此方法后,都需要调用表视图重新加载数据方法。

Hope this helps.

希望这可以帮助。

回答by Dima

Another alternative (to maintain only a copy of the original array), is to use the original array and just grab objects in backwards order in the UITableViewDataSourcefunctions.

另一种选择(仅保留原始数组的副本)是使用原始数组并在UITableViewDataSource函数中按倒序抓取对象。

Example:

例子:

id currentObject = [originalArray objectAtIndex:(originalArray.count - indexPath.row - 1)];

id currentObject = [originalArray objectAtIndex:(originalArray.count - indexPath.row - 1)];

This is actually probably a better solution because you don't need to maintain 2 copies of the same data simply for reversed order.

这实际上可能是一个更好的解决方案,因为您不需要仅仅为了颠倒顺序而维护相同数据的 2 个副本。

回答by hypercrypt

self.yourCurrentArrayOfObjects = ...;
NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:yourCurrentArrayOfObjects.count];
for (id object in yourCurrentArrayOfObjects.reverseObjectEnumerator)
{
    [reversedArray addObject:object];
}
self.yourCurrentArrayOfObjects = [reversedArray copy];
[self.tableView reloadData];

回答by Abhishek

You were using the array to populate the data in table view ...

您正在使用数组在表视图中填充数据......

Sort that array rather than sorting the table order..

对该数组进行排序而不是对表顺序进行排序。