将customTableView添加到视图中,无法正确加载json数据

时间:2014-05-06 22:54:58

标签: ios objective-c json uitableview

在viewController中,我想将我的VoidTableView / addSubview呈现给我的viewController。我在ViewDidLoad中使用了以下代码将它添加到viewController,它确实出现在我的viewController上,但是没有加载数据。

// 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时,表加载但是带有(null)值。当我返回_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 ;
}

我还在VoidViewTable中的ViewDidLoad中放了一个NSLog,但即使我加载表也不会出现。这可能是问题吗?

1 个答案:

答案 0 :(得分:0)

首先,除非您执行逻辑以检查对象是否已添加到子视图,否则永远不要向单元格的内容视图添加添加内容。 Look here for an example

其次,关注者Retterdesdialog的回答并将[tablewView reloadData];放在收到json响应时调用的方法中,例如NSURLConnection的didReceiveResponse:委托方法。

您还需要在viewDidLoad或viewWillAppear中将表视图添加到新创建的视图的子视图中。