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

それは私を示しています: resultsArray: 0: 78

この配列に情報を設定できないのはなぜですか? 次のように、別のコントローラーから refreshTable を呼び出します。

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

更新しました:

tableArray = [NSMutableArray arrayWithArray:resultsArray];

私のために働いた。

その後、私はします

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

それは私を示しています: resultsArray : 0 : 0

tableArray配列が空なのはなぜですか?

4

4 に答える 4

1

オプション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];
}

オプション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];
}

オプション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];
}
于 2012-11-21T17:18:43.370 に答える
1

tableArray次のように変数を設定してみてください:

tableArray = [NSMutableArray arrayWithArray:resultsArray];

可変配列を保持していない可能性があり@propertyます。tableArray を作成することをお勧めします。これをあなたの前に置きます@synthesize

@property (retain, nonatomic) NSMutableArray *tableArray;
于 2012-11-21T08:22:32.420 に答える
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];
}
于 2012-11-21T09:56:19.120 に答える
0

私はそれがうまくいくと信じています

RightViewController *controller = [[RightViewController alloc] init];
[controller refreshTable:resultsArray];
于 2012-11-21T08:30:28.280 に答える