在viewController中,我想将我的VoidTableView呈现/ addSubview到我的viewController上。我在ViewDidLoad中使用了下面的代码将它添加到viewController中,并且它确实出现在我的viewController上,但不加载数据。将一个customTableView添加到视图中,但不能正确加载json数据
// VoidTableView
VoidTableView *controllerVoid;
UITableView *voidTableView ;
controllerVoid = [[VoidTableView alloc]init];
voidTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 222, 481, 400)];
voidTableView.delegate = controllerVoid;
voidTableView.dataSource = controllerVoid;
在我VoidTableView.m文件,我有一个使用一个的WebAPI检索JSON数据,并将其保存到_dataarray的方法。我正在获取数据,但没有显示在表格中。当我为numberOfRows返回一个整数而不是_dataarray.count时,该表将加载但具有(空)值。当我返回_dataarray.count时,表似乎根本没有加载。我想我错过了将数组加载到cellForRowAtIndexPath方法中的关键步骤。有人可以帮忙吗?
编辑:_dataarray是在VoidTableView.h中声明的NSArray属性。我的表格视图代码如下,我在VoidTableView.m中有一个方法loadSalesHistory,将所有数据保存到_dataarray中。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
即使数据被保存到_dataarray,计数仍说,它是0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"////// %lu",(unsigned long)_dataarray.count); // is 0
return [_dataarray count];
// return 3;
}
CellForRowAtIndex应该是标准的大多数人会怎么写。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell ;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
if (cell == nil){
}
UILabel *cusdata = [[UILabel alloc] initWithFrame:CGRectMake(05, 10, 300, cell.bounds.size.height)];
NSString *datestr = [[_dataarray objectAtIndex:indexPath.row] valueForKey:@"Date"];
NSString *timestr = [[_dataarray objectAtIndex:indexPath.row] valueForKey:@"Time"];
NSString *amountstr = [[_dataarray objectAtIndex:indexPath.row] valueForKey:@"SoldAmount"];
amountstr = [NSString stringWithFormat:@"$%@",amountstr];
NSNumber *pointsstr = [[_dataarray objectAtIndex:indexPath.row] valueForKey:@"Points"];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(230, 12, 70, 40)];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(370, 12, 70, 40)];
label2.textAlignment = NSTextAlignmentCenter ;
label3.textAlignment = NSTextAlignmentCenter ;
label2.text = amountstr ;
label3.text = [NSString stringWithFormat:@"%@",pointsstr] ;
[cell.contentView addSubview:label2];
[cell.contentView addSubview:label3] ;
NSString *cusdatastr = [NSString stringWithFormat:@"%@ %@ ",datestr,timestr];
[cell.contentView addSubview:cusdata];
cusdata.text = cusdatastr ;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60 ;
}
我也把一个NSLog的在viewDidLoad中的VoidViewTable但是当我做装载表甚至从来都没有出现。这可能是问题吗?
您提到_dataarray几次你会让我们都不知道它是什么以及它在代码中的用处? – Gruntcakes
@MartinH对不起,我更新了我的文章 – user3513175
您的文章中仍然没有任何内容,只有几行。使用表格视图有几个你必须实现的方法。他们在哪? – Gruntcakes