2013-01-17 49 views
0

我在viewcontroller.h設置的NSMutableArray在viewontrolller.m2 problms與numberOfRowsInSection和的cellForRowAtIndexPath

barcodeArray = [[NSMutableArray alloc] init]; 

這樣的:

@interface SecondViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    NSString *string; 
    NSString *barcode; 
    NSArray *array; 
    NSMutableArray *barcodeArray; 

} 

比我添加對象是這樣的:

if ([barcodeArray indexOfObject:barcode] != NSNotFound) { 
    NSLog(@"%@",barcode); 
    NSLog(@"object zit er al in"); 

} 
else { 
    [barcodeArray addObject:barcode]; 
    [_tableView reloadData]; 
    NSLog(@"%@",barcodeArray); 
    NSLog(@"object zit er nog niet in"); 

} 

當我設置numberOfRowsInSection返回[barcodeArray count];函數cellForRowAtIndexPath不被調用。這就像barcodeArray計數爲null。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [barcodeArray count]; 

} 

當我設置numberofRowsinSection返回1;只是爲了測試..我在它那裏得到與(空)單元:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"aangeroepen"); 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 

     cell.textLabel.text = [NSString stringWithFormat:@"%@",[barcodeArray objectAtIndex:indexPath.row]]; 
NSLog(@"%@", cell.textLabel.text); 


    return cell; 
} 

這裏是我的日誌:

2013-01-17 07:57:04.139 Scanner[21865:c07] (
    123456 
) **// NSMutableArray when i add the barcode** 
2013-01-17 07:57:04.140 Scanner[21865:c07] object zit er nog niet in 
2013-01-17 07:57:05.485 Scanner[21865:c07] viewdidload 
2013-01-17 07:57:05.488 Scanner[21865:c07] aangeroepen 
2013-01-17 07:57:05.489 Scanner[21865:c07] (null) **// NSMutableArray in cell.textLabel.text** 

編輯:

cell.textLabel.text = [NSString stringWithFormat:@"%@",[barcodeArray objectAtIndex:indexPath.row]]; 
NSLog(@"celltext:%@", cell.textLabel.text); 
    NSLog(@"barcodearray%@", [barcodeArray objectAtIndex:indexPath.row]); 
    NSLog(@"objectatindex:0:%@", [barcodeArray objectAtIndex:0]); 
    NSLog(@"indexpath.row:%d", indexPath.row); 

Log: 



2013-01-17 08:27:28.838 Scanner[21978:c07] celltext:(null) 
2013-01-17 08:27:28.839 Scanner[21978:c07] barcodearray(null) 
2013-01-17 08:27:28.839 Scanner[21978:c07] objectatindex:0:(null) 
2013-01-17 08:27:28.839 Scanner[21978:c07] indexpath.row:0 
+0

你在做什麼'barcodeArray = [[NSMutableArray alloc] init];'?你可以在該語句中放置斷點或日誌消息嗎?它可能沒有被執行。 – jrturton

+0

我想我的發送方法是錯誤的。我要去嘗試這個方法:http:// stackoverflow。com/questions/5210535/passing-data-between-view-controllers –

回答

0

條形碼是零。在條形碼中設置一些東西,例如: barCode = @"11225544";並嘗試。

+0

它不是因爲我這樣做: barcode = [a objectAtIndex:0]; NSLog(@「barcode:%@」,barcode); 2013-01-17 08:07:17.944掃描器[21893:c07]條碼:123456 –

+0

試試這個,告訴發生了什麼if([barcodeArray indexOfObject:barcode] == NSNotFound) –

+0

這是什麼? NSLog(@「object zit er al in」); –

1

我有一個非常強烈的感覺,你的barcodeArray只有在你的方法中有一個局部範圍,使用 if/else語句。我認爲您需要保留barcodeArray,因爲您的其他方法無效。

您可以在numberOfRowsInSection委託方法中執行NSLog(@"%d", [barcodeArray count]);並顯示打印內容嗎?這將有助於確認您是如何分配和保留陣列的問題。如果是這樣的問題......那麼在你的頭文件,你會寫,

@property (strong, nonatomic) NSMutableArray* barcodeArray;在實現文件中,你會寫`@synthesize barcodeArray = _barcodeArray;

然後,你會怎麼做,self.barcodeArray = [NSMutableArray alloc] init];

到處都使用barcodeArray,在它之前加上自己。通過保留對象(在頭文件中的屬性... retain = strong),您現在可以在整個課程中訪問該對象。在您的cellForRowAtIndexPathnumberOfRows方法中,它不應該爲空。

編輯:

在代碼中,你正在做

[barcodeArray addObject:barcode]; 

這應該是

[self.barcodeArray addObject:barcode]; 

無處在你的代碼,你應該有barcodeArray - 你應該只self.barcodeArray

+0

它不需要。我用條形碼作爲字符串。雖然我同意它只是一組數字(整數)。 –

+0

不,因爲我設置了我在這裏的答案中評論 –

+0

@Babboe try NSLog(@「%@」,barcode);它來了嗎? NSLog(@「barcode:%@」,barcode); –

相關問題