I have code for a table view that does currently work. It segues perfectly. Does everything I want it to do. Except it does not have alphabetically sorted section titles though. I can't seem to get my code to work with any tutorial on the subject. I'm in need of some suggestions. What I'm looking to specifically do is add a section title for each item alphabetically with a starting letter of that to a section title. If there are no items with starting with say a "P" then I do not want that section title listed. My code is below:
#import "ChoicesTableViewController.h"
#import "MyDataChoices.h"
#import "AddChoiceViewController.h"
@interface ChoicesTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *arrayNames;
@property (strong, nonatomic) NSArray *sections;
@end
@implementation ChoicesTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
//does not work with more than one array in the array
// self.arrayNames = [NSMutableArray arrayWithObjects:
// [NSMutableArray arrayWithArray:@[[MyDataChoices itemWithNewName:@"Apples"]]],
// [NSMutableArray arrayWithArray:@[[MyDataChoices itemWithNewName:@"Oranges"]]], nil];
self.sections = [[NSArray alloc] init];
self.sections = [NSArray arrayWithObjects:@"A",@"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
self.arrayNames = [[NSMutableArray alloc] init];
[self.arrayNames addObjectsFromArray:@[
[MyDataChoices itemWithNewName:@"Apples"],
[MyDataChoices itemWithNewName:@"Bread"]]];
// self.arrayNames = [NSMutableArray arrayWithObjects:
// [MyDataChoices itemWithNewName:@"Apples"],
// [MyDataChoices itemWithNewName:@"Oranges"], nil];
}
//Segue if the item is tapped
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyDataChoices *currentRow = self.arrayNames[indexPath.row];
self.mySelectedCell = currentRow.myNameChoices;
[self performSegueWithIdentifier:@"unwindSegueAction" sender:self];
}
//unwind segue from add choice
- (IBAction)unwindSegueToChoices:(UIStoryboardSegue *)segue
{
AddChoiceViewController *sourceVC = segue.sourceViewController;
NSString *myNewItem = sourceVC.myTextField.text;
//NSString *myFinalString = [[myNewItem substringToIndex:1] capitalizedString];
NSString *stringCapitalized = [myNewItem capitalizedString];
[self.arrayNames addObjectsFromArray:@[[MyDataChoices itemWithNewName:stringCapitalized]]];
[self.tableView reloadData];
}
//titles for talble view
#pragma mark title indexing
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.sections objectAtIndex:section];
}
# pragma mark main table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayNames.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyDataChoices *currentRow = self.arrayNames[indexPath.row];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell2" forIndexPath:indexPath];
cell.textLabel.text = currentRow.myNameChoices;
// NSArray *sectionArray = [self.arrayNames filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.sections objectAtIndex:self.sections]]];
return cell;
}
# pragma Mark delete slide button
//Delete Swipe Button
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
int index = indexPath.row;
[self.arrayNames removeObjectAtIndex:index];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Here is myDataChoices Object code:
+ (MyDataChoices *)itemWithNewName:(NSString *)myNameChoices
{
MyDataChoices *object = [[MyDataChoices alloc] init];
object.myNameChoices = myNameChoices;
return object;
}