来自UITableViw的reloadData不起作用

时间:2012-10-09 21:42:45

标签: iphone ios uitableview tableview

我正在开发应用内购买应用,在获得产品专业应用商店之后,我正在尝试将这些产品加载到uitableview中。问题是:当我得到产品列表时,我无法在表格中加载数据。当我使用静态值创建表而不使用reloadData时,一切正常。

除了加载表格之外,所有东西都正常工作

我的代码在哪里:

@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

4 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分:0)

如果您的数据不在本地,但在某些服务器上,您应该使用此imo。

  

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

另外,我想指出你的小区重用标识符是错误的在一种情况下你有" Cell"在另一种情况下,你有" MyCell"

答案 2 :(得分: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;
    }

答案 3 :(得分:0)

我唯一看到的有点奇怪的是你的tableView:cellForRowAtIndexPath:例程引用了self.productsTableView。但是你已经 in 一个tableView - 为什么你的tableView包含对另一个 tableView的引用?那很奇怪。

同样,您是否检查过self.products实际上包含在tableView中引用它的产品:cellForRowAtIndexPath:和tableView:numberOfRowsInSection:?我无法分辨,因为您没有显示任何显示ivar设置位置的代码。抛出一个NSLog()语句或放置一个断点,并确保它真的包含你认为它的作用。