so first I’ll give some background information so you can understand my problem completely. I’ve spent hours looking for a solution but still can’t seem to get it right. So I’m trying to create an app that loads a tableView as a subview on a uiviewcontroller (SubviewPracticeMasterViewController). The table view is subclassed on another uiviewcontroller(SubviewPracticeViewController), and is loaded as a uiview on SubviewPracticeMasterViewController.
I also have an NSObject called LeadsInformation. LeadsInformation.h looks like this
#import <Foundation/Foundation.h>
@interface LeadsInformation : NSObject
@property (nonatomic, copy)NSString *leadName;
@property (nonatomic, copy)NSString *lastName;
@property (nonatomic, copy)NSString *firstName;
@property (nonatomic, copy)NSString *areaCode;
@property (nonatomic, copy)NSString *phoneNumber;
@property (nonatomic, copy)NSString *status;
@property (nonatomic, copy)NSString *notes;
-(id)initWithName: (NSString *)leadName lastName: (NSString *)lastName firstName: (NSString *)firstName
areaCode: (NSString *)areaCode phoneNumber: (NSString *)phoneNumber status: (NSString *)status
notes: (NSString *)notes;
@end
So on SubviewPracticeViewController.m, I programmatically add 4 LeadsInformation objects into an array called ‘dataArray’ and load specific string values into a custom cell in my cellForRowAtIndexPath method.
Now this is where my problem is occurring. I am also loading a custom header from a xib file. The header has two buttons on it: I want the first button to sort the cells alphabetically based on the first column, and I want the second button to sort the cells alphabetically based on the second column of the cell.
The table view is showing exactly as I want it to, with the custom table header as well, and the buttons are being clicked. However, the cells are not getting sorted. I have put the code for sorting by the first column in the IBAction method associated with its button on the header. Here is the code:
- (IBAction)sortLeadName:(id)sender {
NSSortDescriptor *leadNameDescriptor = [[NSSortDescriptor alloc] initWithKey: @"leadName" ascending:YES selector:@selector(localizedStandardCompare:)];
NSArray *sortDescriptor = [NSArray arrayWithObject:leadNameDescriptor];
NSArray *sortedArray = [self.dataArray sortedArrayUsingDescriptors:sortDescriptor];
self.dataArray = [[NSMutableArray alloc] initWithArray:sortedArray];
[[self tableView] reloadData];
}
And here is the code for cellForRowAtIndexPath just in case:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"Identifier"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
LeadsInformation *leadAtIndex = [self.dataArray objectAtIndex: indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = leadAtIndex.leadName;
label = (UILabel *)[cell viewWithTag:2];
label.text = leadAtIndex.status;
return cell;
}
Any help would be greatly appreciated. Thanks!
So it turns out I was doing some unnecessary work that was throwing me off. I didn’t need to sort a copy of my array and then reload my dataArray as the new sorted copy. I just needed to directly sort the array with the SortDescriptor, like so: