2014-05-06 30 views
-3

在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但是當我做裝載表甚至從來都沒有出現。這可能是問題嗎?

+0

您提到_dataarray幾次你會讓我們都不知道它是什麼以及它在代碼中的用處? – Gruntcakes

+0

@MartinH對不起,我更新了我的文章 – user3513175

+0

您的文章中仍然沒有任何內容,只有幾行。使用表格視圖有幾個你必須實現的方法。他們在哪? – Gruntcakes

回答

0

首先,絕對不要添加東西到單元格的內容視圖中,除非您執行邏輯來檢查對象是否已添加到子視圖。 Look here for an example

其次,跟隨Retterdesdialog的答案,並把[tablewView reloadData];在被調用的方法,當你收到你的JSON響應,如didReceiveResponse:爲NSURLConnection的委託方法。

您還需要將表視圖添加到viewDidLoad或viewWillAppear中新創建的視圖的子視圖中。

相關問題