我在 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 count]; 未调用函数 cellForRowAtIndexPath。就像barcodeArray 计数为空一样。
- (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