表视图中的节使用对象数组

时间:2014-11-03 09:32:28

标签: ios objective-c iphone xcode uitableview

我有一个目前正常工作的表视图的代码。它很完美。我想要它做的一切。但它没有按字母顺序排序的部分标题。我似乎无法使用我的代码来处理有关该主题的任何教程。我需要一些建议。我特别要做的是按字母顺序为每个项目添加一个部分标题,并在部分标题中添加该部分标题。如果没有以“a" P"然后我不希望列出该部分标题。我的代码如下:

#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

这是myDataChoices对象代码:

+ (MyDataChoices *)itemWithNewName:(NSString *)myNameChoices
{
    MyDataChoices *object = [[MyDataChoices alloc] init];

    object.myNameChoices = myNameChoices;


    return object;
}

0 个答案:

没有答案