У меня проблема с обновлением объекта 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
Я делаю почти то же самое надругое мнение, и сравнили, но я не вижу никакой разницы, которая могла бы вызвать проблему.