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

它告诉我:结果数组: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

尝试tableArray像这样设置变量:

tableArray = [NSMutableArray arrayWithArray:resultsArray];

您可能不会保留您的可变数组,我建议@property您为您的 tableArray 创建一个。把它放在你的前面@synthesize

@property (retain, nonatomic) NSMutableArray *tableArray;
于 2012-11-21T08:22:32.420 回答
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 回答
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 回答