2012-02-04 47 views
0

我在加載後更新UITableView對象時遇到問題。我有一種感覺,它與異步數據有關,但我不知所措。表代表和數據源被設置爲UIView(CartViewController)。UITableView在LoadData調用後不顯示數據

.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 

我做幾乎同樣的事情在另一個視圖,並進行比較,但我沒有看到將導致該問題的任何差異。

回答

0

我認爲你的表會重新加載之前URL的數據arives。嘗試在請求URL中的數據的線程中重新加載數據。嘗試刪除第二個dispatch_async。

+0

我刪除了第二個dispatch_async並沒有任何東西。我甚至刪除了第一個dispatch_async,剛剛在viewdidload上獲得了數據,但仍然沒有任何結果。數據以預期的結果返回(我可以在NSLog中看到它)。 – DJH 2012-02-05 16:58:45

+0

嘗試把'NSLog(@「返回的對象數量:%i」,[myCartItems count]);'返回之前,在' - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection'部分並檢查日誌是否存在是0以外的東西; – 2012-02-05 20:02:40

+0

它被調用兩次,一次在初始視圖加載和一次當我調用表視圖重新加載。第一次返回0,因爲我沒有加載數據。第二次返回預期金額(本例中爲1)。 – DJH 2012-02-06 15:16:30

0

確保您已將所有IBOutlet連接到接口構建器中。

+0

IBOutlets已連接(僅用於tableView)。 – DJH 2012-02-04 21:57:17