Sort the array of objects in tableview
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?
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!
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];
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;
}
}
精彩评论