-1

I want to add an new object to my array each time I enter in the UITableView. The problem is that the UITableView get dealloc'd when I go out from this view so I can't declare my array in the UITableView class.

I create a new NSObject class called "array" but I don't know how to use it.

Array.h

#import <Foundation/Foundation.h>

@interface Array : NSObject
{
    NSMutableArray *tableau;
}

@property (strong) NSMutableArray* tableau;
- (id)initWithName:(NSMutableArray *)atableau  ;

- (NSMutableArray*) tableau;

- (void) setTableau:(NSMutableArray*) newTableau;

+(Tableau*)instance;

@end

Array.m

#import "Array.h"

@implementation Array

- (id)initWithName:(NSMutableArray *)atableau {
    if ((self = [super init]))

    {
        self.tableau = atableau;
    }
    return self;

}

- (NSMutableArray*) tableau{
    return tableau;
}

- (void) setTableau:(NSMutableArray*) newTableau{
    tableau = newTableau;
}

+(Tableau*)instance{
    static dispatch_once_t once;
    static Array *sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] initWithName:@"jean" ];
    });
    return sharedInstance;
}
@end

UITableViewController.m

...
- (void)viewDidAppear:(BOOL)animated
{
    if (![[Array instance] tableau]) {

    }
    [[[Array instance]tableau]addObject:@"koko"];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    NSLog(@"appear");

}

...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[[Array instance] tableau] removeObjectAtIndex:indexPath.row];
        [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.
    }
...

When I do that I got this error :

'NSInvalidArgumentException', reason: '-[__NSCFConstantString addObject:]: unrecognized selector sent to instance 0x5aa4'

Thank you for your future response.

Fonzie f
  • 117
  • 4
  • Try with NSUserDefaults , To store and display when come back to the View .... – Kumar KL Nov 12 '13 at 09:18
  • here's a short description on how to use singleton object as datamodel: http://stackoverflow.com/a/16199974/653513 – Rok Jarc Nov 12 '13 at 09:20
  • You are doing it correctly you might try to add data into array before you go to `UITableViewController` – iphonic Nov 12 '13 at 09:22
  • What is your question? I'd do details differently in that singleton but that is rather a question of personal style. Your code looks fine to me. Just some comments. viewWill/DidAppear are called any time when the table comes to view. That includes returns from views that were pushed or presented on top and the app returning from background and the user navigating back into a tab (in a tabview app). If that is what you want, then it is fine. Second you add the names to the top of the table but you add them to the bottom of your array. That, too, may be, what you want and may well be ok. – Hermann Klecker Nov 12 '13 at 09:27
  • the problem is that I have errors when I enter to the UITableView: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString addObject:]: unrecognized selector sent to instance 0x5aa4' – Fonzie f Nov 12 '13 at 09:31

1 Answers1

1

Problem in this line of your code:

sharedInstance = [[self alloc] initWithName:@"jean" ];

In result you assign NSString instance instead of NSMutableArray

- (id)initWithName:(NSMutableArray *)atableau {
    self = [super init];
    if (self) {
       self.tableau = atableau;
    }
    return self;
}

Change it to something like:

sharedInstance = [[self alloc] initWithName:[[NSMutableArray alloc] initWithObjects:@"jean", nil]];
Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
Ruslan Soldatenko
  • 1,718
  • 17
  • 25