2012-10-09 69 views
-1

我正在開發一個應用內購買應用程序,並獲得產品親應用程序商店後,我試圖加載這些產品在一個可用視圖。問題是:當我得到產品列表時,我無法在表格中加載數據。當我使用靜態值創建表而不使用reloadData時,一切正常。從UITableViw reloadData不起作用

Eeverything工作正常旁裝載表

這裏是我的代碼:

@synthesize products; 
@synthesize productsTableView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 

    NSSet *productIndentifiers = [NSSet setWithObjects:@"Prod01",@"Prod02",@"Prod03",nil]; 
    productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIndentifiers]; 
    [productsRequest start]; 

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE]; 
    [super viewDidLoad]; 

} 

- (void)viewDidUnload 
{ 
    [self setProductsTableView:nil]; 
    [super viewDidUnload]; 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - UITableViewDelegate 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.products.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = nil; 
    [self.productsTableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if(cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]; 
     SKProduct *product = [self.products objectAtIndex:indexPath.row]; 
     cell.textLabel.text = product.localizedTitle; 

    } 

    return cell; 
} 

#pragma mark - SKProductsRequestDelegate 

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response 
{ 
    NSLog(@"%@", response.products); 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; 
    [self.productsTableView reloadData]; 

} 


- (void)dealloc { 
    [productsTableView release]; 
    [super dealloc]; 
} 
@end 

回答

0

你錯過了加入SKProductsRequest的結果self.products

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response 
{ 
    NSLog(@"%@", response.products); 
self.product = assign data here 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; 
    [self.productsTableView reloadData]; 

} 
0

如果數據不在本地但在某些服務器上,您應該使用此imo。

[tableView beginUpdate];
[tableView insertRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation:rowAnimation];
[tableView endUpdate];

同時,我想指出的是您的單元複用標識符是錯誤的。在一個情況下,你有「細胞」,並在其他情況下,你有「了myCell」

0

有幾個問題。 NeverBe和S.P.已經注意到了一些。這是另一個主要的。

的一個問題是的cellForRowAtIndexPath:

你只在單元格中時,其新的設定數據。表視圖回收單元格,所以有些會跳過你的if語句。那些將無法正確安裝。將您的單元格設置移到if語句之外。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     UITableViewCell *cell = nil; 
     [self.productsTableView dequeueReusableCellWithIdentifier:@"Cell"]; 
     if(cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]; 

     } 

     SKProduct *product = [self.products objectAtIndex:indexPath.row]; 
     cell.textLabel.text = product.localizedTitle; 

     return cell; 
    } 
+0

謝謝,可能是有另一個問題,當我修復它,我刪除了產品數組歸屬。 – Rafael

0

我看到這是一個有點奇怪的唯一的事情就是你的tableView:的cellForRowAtIndexPath:常規引用self.productsTableView。但你已經一個tableView - 爲什麼你的tableView包含一個引用另一個 tableView?這很奇怪。

同樣,你是否檢查過self.products實際上是否包含在tableView:cellForRowAtIndexPath:和tableView:numberOfRowsInSection中引用的產品:?由於您沒有顯示任何代碼顯示設置的ivar,因此我無法判斷。拋出一個NSLog()語句或放置一個斷點,並確保它確實包含您認爲它的作用。

相關問題