ロード後にUITableViewオブジェクトの更新に問題があります。私はそれが非同期データと関係があると感じていますが、私は迷っています。テーブル代理人とデータソースはUIView(CartViewController)に設定されています。私は別のビューで、ほぼ正確に同じことを行う、と比較しているが、私は問題を引き起こす任意の違いを見ていないよ.HLoadData呼び出し後にUITableViewがデータを表示しない
@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番目のdispatch_asyncを削除しましたが、何もありません。私も最初のdispatch_asyncを削除し、ちょうどviewdidloadのデータを得ました。データは期待通りの結果を返しています(私はNSLogを見るとそれを見ることができます)。 – DJH
'NSLog(@)返されるオブジェクトの数:%i"、[myCartItems count]); ' - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection'セクションに戻り、そこにログがあればチェックインしてください0以外のものです。 –
初期ビューのロード時に1回、テーブルビューでリロードを呼び出すときに2回呼び出されます。私はデータをロードしていないので、最初に0が返ってきます。 2番目は期待された量(この場合は1)に戻ります。 – DJH