0
@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?

4
  • r u tring like tablearray=resultsArray Commented Nov 21, 2012 at 8:22
  • Are you sure your resultsArray is not nil or have no items? Commented Nov 21, 2012 at 8:34
  • It doesn't work because your ivar is always an empty array, and I guess that your tableView data source is baked by said array. Commented Nov 21, 2012 at 8:34
  • how about a strong array property ?? Commented Mar 20, 2017 at 12:52

4 Answers 4

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Worked for me. Can you see that is wrong with updated info in a question?
That should work better. But the reason is not that setArray was wrong in any way. The reason might be that viewDidLoad may not have been called before refreshTable is executed. And Victor did not initialize his array in init but in view did load. Therefore setArray may have been sent to a nil object and count may have been sent to a nil object. It should therefore return 0 nor nil. Your suggestion is fine and moving tableArray = [[NSMutableArray alloc] init]; to the init method should work as well.
1

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];
}

Comments

0

i believe it should work

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

2 Comments

Your answer would be considerably better if you could explain why.
I would not sign in blood that viewDidLoad has been called before refreshTable is invoked.
0
@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];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.