对 tableview 中的对象数组进行排序

发布于 2024-12-02 01:08:49 字数 98 浏览 0 评论 0原文

我正在开发一个 iPad 应用程序,它有一个带有自定义单元格的表格视图。 每个单元格都是一个对象,用 3 列表示该类的属性。 我想根据列对数据进行排序。我怎样才能使用目标c来实现它?

Iam developing an iPad application, It has a tableview with custom cells.
Each cell is an object which represents the properties of the class in 3 columns.
I want to sort the data based on the columns. How can I achieve it using objective c?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠意 2024-12-09 01:08:49

您可以使用 NSSortDescriptor 来完成此工作。

准备需要排序的数组

NSArray *objects = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil];

使用 NSSortDescriptors 创建一个数组,在本例中我使用 1。对于 DescriptorKey,从您希望排序所依据的 Cell 中获取一个属性。

NSArray *descriptor = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];

然后实例化一个新数组,并在初始数组上调用 NSArray 中的sortedArrayUsingDescriptors 实例方法,返回具有相同对象的排序数组。

NSArray *sortedArray = [objects sortedArrayUsingDescriptors:descriptor];

希望这有帮助!

You can use a NSSortDescriptor to do this job.

Prepare the array that needs sorting

NSArray *objects = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil];

Create an array with NSSortDescriptors, in this example I use 1. For the DescriptorKey, take a property from the Cell on which you would like the sorting to be based upon.

NSArray *descriptor = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];

Then instantiate a new array and call the sortedArrayUsingDescriptors instance method from NSArray, on your initial array, returning a sorted array with the same objects.

NSArray *sortedArray = [objects sortedArrayUsingDescriptors:descriptor];

Hope this helps!

深爱不及久伴 2024-12-09 01:08:49

据我了解,UITableView 中没有“列”。人们将格式良好的子视图放入 TableViewCell 中,以使表格看起来像有列。

要对数组进行排序,您可以尝试上面的 Bryan 方法。得到排序后的数组调用后

  [TheTableView reloadData];

For my understanding, there is no "column" in UITableView. People put well formatted subviews in the TableViewCell, to make the table look like have columns.

For sorting the Array, you may try Bryan's method above. After you get the sorted array call

  [TheTableView reloadData];
泛滥成性 2024-12-09 01:08:49

我在多个 NSMutableArrays 和许多自定义单元格中遇到了类似的情况。我必须根据各个列对它们进行排序,并且我进行了全面搜索,但找不到解决方案。
这是我的:

从列上的 UIButton 发送两个参数(排序和升序/降序),并将每个“ROW”的所有内容保存到字典中。

-(void)sortTheArray :(NSString *)sortWith :(BOOL)isAsc
{
sortedArray = [[NSMutableArray alloc]init];
NSDictionary *dict;

for (int i =0; i< orderArr.count; i++) {
    dict = [NSDictionary dictionaryWithObjectsAndKeys:[addressArr objectAtIndex:i], @"names", [zipArr objectAtIndex:i], @"zip", [orderArr objectAtIndex:i], @"orderNo", [cityArr objectAtIndex:i], @"city", nil];

    [sortedArray addObject:dict];
    //   NSLog(@"Sorted Array: %@", [sortedArray objectAtIndex:i]);
}

现在,根据点击的列,我使用排序描述符对我们刚刚创建的字典进行排序。

NSSortDescriptor *sortDescript = [[NSSortDescriptor alloc]initWithKey:sortWith ascending:isAsc];
NSArray *descriptors = [NSArray arrayWithObject:sortDescript];
NSArray *newSortedArray = [sortedArray sortedArrayUsingDescriptors:descriptors];

我重新初始化现有的可变数组,使它们为零。

orderArr = [[NSMutableArray alloc]init];
zipArr = [[NSMutableArray alloc]init];
addressArr = [[NSMutableArray alloc]init];
cityArr = [[NSMutableArray alloc]init];

在这里,我开始将排序数组的内容添加回旧的 NSMutableArrays 并重新加载表。

for (int i = 0; i<newSortedArray.count; i++) {
    // NSLog(@"New Sorted Array is : %@", [newSortedArray objectAtIndex:i]);
    orderArr = [newSortedArray valueForKey:@"orderNo"];
    zipArr = [newSortedArray valueForKey:@"zip"];
    addressArr = [newSortedArray valueForKey:@"names"];
    cityArr = [newSortedArray valueForKey:@"city"];

    }
[self.tableObject reloadData];
}

如果您需要它,我的 UIButton 如下所示:

- (IBAction)sortWithOrderNo:(id)sender;
    {
if (isAscending == YES) {
    [self sortTheArray:@"orderNo":YES];
    isAscending = NO;
    }
else
    {
    [self sortTheArray:@"orderNo":NO];
    isAscending = YES;
    }
}

I had a similar situation with multiple NSMutableArrays and many custom cells. I had to sort them based on various columns and I searched all over and couldn't find a solution.
So here's mine:

From a UIButton on the column, I send two parameters(sortwith and ascending/descending), and save all the contents of each "ROW" into a dictionary.

-(void)sortTheArray :(NSString *)sortWith :(BOOL)isAsc
{
sortedArray = [[NSMutableArray alloc]init];
NSDictionary *dict;

for (int i =0; i< orderArr.count; i++) {
    dict = [NSDictionary dictionaryWithObjectsAndKeys:[addressArr objectAtIndex:i], @"names", [zipArr objectAtIndex:i], @"zip", [orderArr objectAtIndex:i], @"orderNo", [cityArr objectAtIndex:i], @"city", nil];

    [sortedArray addObject:dict];
    //   NSLog(@"Sorted Array: %@", [sortedArray objectAtIndex:i]);
}

Now based on which column was tapped, I use a sort descriptor to sort the dictionary that we just created.

NSSortDescriptor *sortDescript = [[NSSortDescriptor alloc]initWithKey:sortWith ascending:isAsc];
NSArray *descriptors = [NSArray arrayWithObject:sortDescript];
NSArray *newSortedArray = [sortedArray sortedArrayUsingDescriptors:descriptors];

I reinitialize the existing Mutable arrays so that they are nil.

orderArr = [[NSMutableArray alloc]init];
zipArr = [[NSMutableArray alloc]init];
addressArr = [[NSMutableArray alloc]init];
cityArr = [[NSMutableArray alloc]init];

Here I start adding the contents of the sorted array back to old NSMutableArrays and reload the table.

for (int i = 0; i<newSortedArray.count; i++) {
    // NSLog(@"New Sorted Array is : %@", [newSortedArray objectAtIndex:i]);
    orderArr = [newSortedArray valueForKey:@"orderNo"];
    zipArr = [newSortedArray valueForKey:@"zip"];
    addressArr = [newSortedArray valueForKey:@"names"];
    cityArr = [newSortedArray valueForKey:@"city"];

    }
[self.tableObject reloadData];
}

Incase you need it, my UIButton looks like this:

- (IBAction)sortWithOrderNo:(id)sender;
    {
if (isAscending == YES) {
    [self sortTheArray:@"orderNo":YES];
    isAscending = NO;
    }
else
    {
    [self sortTheArray:@"orderNo":NO];
    isAscending = YES;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文