简体   繁体   中英

2 problems with numberOfRowsInSection and cellForRowAtIndexPath

I set a NSMutableArray in viewcontrolller.m

barcodeArray = [[NSMutableArray alloc] init];

in viewcontroller.h like this:

@interface SecondViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

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

}

Than i add an object like this:

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");

}

When i set numberOfRowsInSection to return [barcodeArray count]; the function cellForRowAtIndexPath isnt called. it's like the barcodeArray count is null.

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

    return [barcodeArray count];

}

When i set numberofRowsinSection to return 1; just to test.. i get a cell with (null) in it:

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

Here's my log:

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**

EDIT:

    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

I have a very strong feeling that your barcodeArray only has a local scope in your method with if/else statements. I think that you need to retain the barcodeArray , because it's null in your other methods.

Can you do an NSLog(@"%d", [barcodeArray count]); in your numberOfRowsInSection delegate method and show what that prints? That'll help confirm if it's an issue with how you allocated and retain the array. If that's the problem...then in your header file, you would write,

@property (strong, nonatomic) NSMutableArray* barcodeArray; and in your implementation file, you would write `@synthesize barcodeArray = _barcodeArray;

Then, you would do, self.barcodeArray = [NSMutableArray alloc] init];

and everywhere you use barcodeArray , add self before it. By retaining the object (with the property in your header file... retain = strong), you can now access is throughout the class. It shouldn't be null anymore in your cellForRowAtIndexPath and numberOfRows methods.

EDIT:

In your code, you're doing

[barcodeArray addObject:barcode];

This should be

[self.barcodeArray addObject:barcode];

nowhere in your code should you have barcodeArray -- you should only have self.barcodeArray .

The barcode is nil . Set something in the barcode ie; barCode = @"11225544"; and try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM