在LoadData调用后,UITableView不显示数据

时间:2012-02-04 21:39:58

标签: iphone uitableview ios5

我在加载后更新了UITableView对象的问题。我有一种感觉它与异步数据有关,但我不知所措。表委托和数据源设置为UIView(CartViewController)。

·H

@interface CartViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *myCart;
    NSArray *myCartItems;
    UITableView *tableView;
}

@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSDictionary *myCart;
@property (nonatomic, retain) NSArray *myCartItems;
- (IBAction)RemoveFromCart:(id)sender;

@end

的.m

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)

#import "CartViewController.h"
#import "VariablesStore.h"

@implementation CartViewController
@synthesize myCart,myCartItems;
@synthesize tableView;


- (void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[VariablesStore sharedInstance] CartURL]]];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];

       dispatch_async(dispatch_get_main_queue(), ^{

          [self.tableView reloadData];

       });

    });

}


- (void)fetchedData:(NSData *)responseData
{

    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSDictionary* cart = [json objectForKey:@"Cart"];
    myCart = cart;

    NSArray *itemList = [cart objectForKey:@"CartItemList"];
    myCartItems = itemList;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.

    return [myCartItems count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


    UILabel *skuNum = (UILabel *)[cell viewWithTag:1];
    NSDictionary *cartItem = [myCartItems objectAtIndex:indexPath.row];

    NSLog(@"Dictionary: %@", cartItem); // returns the cart item
    NSLog(@"SkuFormatted: %@", [cartItem valueForKey:@"SKUFormatted"]); //shows the sku correctly
    [skuNum setText:[cartItem valueForKey:@"SKUFormatted"]];
    NSLog(@"skuNum:%@", skuNum.text); //shows (null) for the sku


    return cell;
}



@end

我在另一个视图上做的几乎完全相同,并进行了比较,但我没有看到任何会导致问题的差异。

2 个答案:

答案 0 :(得分:0)

确保在界面构建器中连接了所有IBOutlet。

答案 1 :(得分:0)

我认为你的表会在来自URL的数据之前重新加载。尝试在您从URL请求数据的线程中重新加载数据。尝试删除第二个dispatch_async。

相关问题