Dark Matter
Dark Matter

Reputation: 3698

Set NSMutableArray to the new NSMutableArray

@implementation RightViewController{
    NSMutableArray * tableArray;
}

@synthesize tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    tableArray = [[NSMutableArray alloc] init];
}


- (void)refreshTable: (NSMutableArray*)resultsArray{
    [tableArray setArray:resultsArray];    
    NSLog(@"resultsArray : %d : %d", [tableArray count] , [resultsArray count]);

    [self.tableView reloadData];
}

It shows me : resultsArray : 0 : 78

Why can't I set information to this array? I do call refreshTable from another controller like this:

[[[RightViewController alloc] init] refreshTable:resultsArray];

Updated:

tableArray = [NSMutableArray arrayWithArray:resultsArray];

worked for me.

After that i do

- (IBAction)reloadTableButton:(id)sender {
    [self refreshTable:tableArray];
}

and it's shows me : resultsArray : 0 : 0

Why is tableArray array empty?

Upvotes: 0

Views: 150

Answers (4)

Hermann Klecker
Hermann Klecker

Reputation: 14068

Option 1:

- (void)viewDidLoad {
    [super viewDidLoad];
    //don't do anything with the array here! 
    //refreshTable may well be called before the view is loaded
}


- (void)refreshTable: (NSMutableArray*)resultsArray{

    if (!self.tableArray) // if it does not exist, then create it on the fly. 
        self.tableArray = [[NSMutableArray alloc] init];
    [tableArray setArray:resultsArray];    
    [self.tableView reloadData];
}

Option 2:

- (MyClass*) init {
  self = [super init];
  if (self) {
     self.tableArray = [[NSMutableArray alloc] init];
  }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //don't do anything with the array here! 
}


- (void)refreshTable: (NSMutableArray*)resultsArray{
    [tableArray setArray:resultsArray]; // you can be sure that init was invoked earlier.    
    [self.tableView reloadData];
}

Option 3:

- (void)viewDidLoad {
    [super viewDidLoad];
    //don't do anything with the array here! 
}


- (void)refreshTable: (NSMutableArray*)resultsArray{
    self.tableArray = [NSMutalbeArray arrayWithArray:resultsArray];   //This creates a new array as a copy of resultsArray and assigns it to tableArray. No need to initialize anything. 
    [self.tableView reloadData];
}

Upvotes: 1

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

@implementation RightViewController{
    NSMutableArray * tableArray;
}
@property (strong, nonatomic) NSMutableArray *tableArray; //use the keyword "retain" instead of "strong" in below iOS 5

@synthesize tableView;
@synthesize tableArray;

- (void)viewDidLoad {

    [super viewDidLoad];
    self.tableArray = [[NSMutableArray alloc] init];
}


- (void)refreshTable: (NSMutableArray*)resultsArray{

     self.tableArray = resultsArray;

    NSLog(@"resultsArray : %d : %d", [tableArray count] , [resultsArray count]);

    [self.tableView reloadData];
}

Upvotes: 0

Yuliani Noriega
Yuliani Noriega

Reputation: 1083

Try setting your tableArray variable like so :

tableArray = [NSMutableArray arrayWithArray:resultsArray];

You might not be retaining your mutable array I would suggest making a @property for your tableArray. Place this before your @synthesize

@property (retain, nonatomic) NSMutableArray *tableArray;

Upvotes: 1

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

i believe it should work

RightViewController *controller = [[RightViewController alloc] init];
[controller refreshTable:resultsArray];

Upvotes: 0

Related Questions