2012-11-21 56 views
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設置的NSMutableArray新的NSMutableArray

爲什麼我不能設置信息這陣? 我這樣從另一個控制器調用refreshTable:

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

更新時間:

tableArray = [NSMutableArray arrayWithArray:resultsArray]; 

爲我工作。

後,我做的

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

,它的顯示我:resultsArray:0:0

爲什麼tableArray數組是空的?

+0

[Rü特林像tablearray = resultsArray –

+0

@yulz%d是INT –

+0

你確定你的resultsArray不爲零或者沒有項目? –

回答

1

嘗試設置你的tableArray變量,像這樣:

tableArray = [NSMutableArray arrayWithArray:resultsArray]; 

你可能不會保留你的可變數組我建議作出@property您tableArray。將在此之前你@synthesize

@property (retain, nonatomic) NSMutableArray *tableArray; 
+0

爲我工作。你能看到這個問題中的更新信息是錯誤的嗎? –

+0

這應該會更好。但是,原因並不是說setArray在任何方面都是錯誤的。原因可能是viewDidLoad在refreshTable執行之前可能沒有被調用過。並且Victor在初始化時沒有初始化他的數組,但在視圖中加載。因此,setArray可能已發送給一個零對象,並且計數可能已發送給一個零對象。因此它應該返回0或零。你的建議是好的,移動'tableArray = [[NSMutableArray alloc] init];'init方法也應該可以。 –

0

我相信它應該工作

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

如果你能解釋_why_,你的答案會好得多。 – Ben

+0

我不會在調用refreshTable前調用viewDidLoad的血跡​​。 –

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

它仍然顯示0(( –

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