0

viewcontrolller.m に NSMutableArray を設定しました

barcodeArray = [[NSMutableArray alloc] init];

次のようにviewcontroller.hで:

@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 カウント] を返すように設定すると、関数 cellForRowAtIndexPath は呼び出されません。これは、barcodeArray のカウントが null のようです。

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

    return [barcodeArray count];

}

numberofRowsinSection を 1 に設定すると、テストするだけです..(null)を含むセルを取得します:

- (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
4

2 に答える 2

1

barcodeArrayif/else ステートメントを使用したメソッドには、ローカル スコープしかないことが非常に強く感じられます。を保持する必要があると思いますbarcodeArray。他のメソッドでは null であるためです。

NSLog(@"%d", [barcodeArray count]);あなたのnumberOfRowsInSectionデリゲートメソッドで実行して、それが何を印刷するかを示すことができますか? これは、配列を割り当てて保持する方法に問題があるかどうかを確認するのに役立ちます。それが問題である場合は、ヘッダー ファイルに次のように記述します。

@property (strong, nonatomic) NSMutableArray* barcodeArray;実装ファイルに、次のように記述します。`@synthesize barcodeArray = _barcodeArray;

次に、あなたはするでしょう、self.barcodeArray = [NSMutableArray alloc] init];

を使用するすべての場所barcodeArrayで、その前に self を追加します。オブジェクトを保持することにより (ヘッダー ファイルのプロパティを使用して ... 保持 = 強力)、クラス全体で is にアクセスできるようになりました。cellForRowAtIndexPathandnumberOfRowsメソッドでは、もう null であってはなりません。

編集:

あなたのコードでは、あなたはやっています

[barcodeArray addObject:barcode];

これは

[self.barcodeArray addObject:barcode];

コードのどこにbarcodeArrayself.barcodeArray.

于 2013-01-17T07:14:21.930 に答える
0

バーコードは nil です。バーコードに何かを設定します。barCode = @"11225544";試してみてください。

于 2013-01-17T07:13:56.270 に答える