UITableView не показывает данные после вызова LoadData - PullRequest
0 голосов
/ 05 февраля 2012

У меня проблема с обновлением объекта 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 голосов
/ 05 февраля 2012

Я думаю, что ваша таблица будет перезагружена до того, как данные из URL появятся. Попробуйте перезагрузить данные в потоке, где вы запрашиваете данные из URL. Попробуйте удалить второй dispatch_async.

0 голосов
/ 05 февраля 2012

Убедитесь, что все ваши IBOutlets подключены в конструкторе интерфейсов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...