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.