0

我有一个应用程序,我必须从互联网上获取信息。对 url 的请求在一个块中,这是在 UIViewController 出现之后进行的,这使得我的应用程序在运行时崩溃,因为我使用该信息来构造 UIViewController。

我不知道如何确保代码块完成他的任务并稍后使用信息。

*编辑*

现在我的应用程序显示一个空表,我不知道如何显示我得到的信息。也许可以使用 reloadTable,但我不知道如何。帮助!

StoreController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    _productData = [ProductData ProductData];
    [_productData backEndTest];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_listOfChips count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductCellController *cell = (ProductCellController*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            if (cell == nil)
            {
                NSString* nameNib = UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM() ? @"ProductCellController" : @"ProductCellControllerIphone";
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nameNib owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            cell = (ProductCellController*)[self cellFormat:cell atTheIndex:indexPath withThelastIndex:pathToLastRow];
            cell.image.image = [UIImage imageNamed:[((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])image]];
            cell.title.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])title]];
            cell.price.text = [NSString stringWithFormat:@"$ %@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])price]];
            cell.amount.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])quantity]];
}

产品数据.m

+(ProductData*)ProductData
{
    ProductData* data = [[ProductData alloc]init];
    return data;
}
- (void)backEndTest
{

    [Server getProductsWithCompletionHandler:^(id result, NSError* error)
     {
         if ( error )
         {
             NSLog(@"error %@", error);
         }
         else
         {
             //NSMutableArray* arrayProducts = [NSMutableArray array];
             int index = 0;
             for (NSDictionary* product in result)
             {
                 [_listOfProducts addObject:[[Products alloc] initWithProduct:[chip objectForKey:GSR_PARAM_PRODUCT_ID]
                                                          withTitle:[product objectForKey:GSR_PARAM_PRODUCT_DESCRIPTION]
                                                         numberUses:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
                                                          withPrice:[product objectForKey:GSR_PARAM_PRODUCT_PRICE]
                                                       withQuantity:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
                                                        withInAppID:[product objectForKey:GSR_PARAM_PRODUCT_IN_APP_ID]
                                                          withImage:[[self loadImages]objectAtIndex:index ]] ];                    
                 index++;
             }
         }
     }];

}

我不知道如何管理街区并确保他完成。有任何想法吗?提前致谢!

4

4 回答 4

0

另一种解决方案可以是:

在 ProductData.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    _productData = [ProductData ProductData];
    [self loadDataFromServer:^{
        [self.tableView reloadData];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_listOfChips count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductCellController *cell = (ProductCellController*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            if (cell == nil)
            {
                NSString* nameNib = UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM() ? @"ProductCellController" : @"ProductCellControllerIphone";
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nameNib owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            cell = (ProductCellController*)[self cellFormat:cell atTheIndex:indexPath withThelastIndex:pathToLastRow];
            cell.image.image = [UIImage imageNamed:[((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])image]];
            cell.title.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])title]];
            cell.price.text = [NSString stringWithFormat:@"$ %@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])price]];
            cell.amount.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])quantity]];
}

- (void)loadDataFromServer
{

    [Server getProductsWithCompletionHandler:^(id result, NSError* error)
     {
         if ( error )
         {
             NSLog(@"error %@", error);
         }
         else
         {
             //NSMutableArray* arrayProducts = [NSMutableArray array];
             int index = 0;
             for (NSDictionary* product in result)
             {
                 [_listOfProducts addObject:[[Products alloc] initWithProduct:[chip objectForKey:GSR_PARAM_PRODUCT_ID]
                                                          withTitle:[product objectForKey:GSR_PARAM_PRODUCT_DESCRIPTION]
                                                         numberUses:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
                                                          withPrice:[product objectForKey:GSR_PARAM_PRODUCT_PRICE]
                                                       withQuantity:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
                                                        withInAppID:[product objectForKey:GSR_PARAM_PRODUCT_IN_APP_ID]
                                                          withImage:[[self loadImages]objectAtIndex:index ]] ];                    
                 index++;
             }
         }
     }];

}
于 2013-08-01T15:06:10.230 回答
0

您可以创建一个完成块并在完成块中创建视图控制器。而不是在完成下载数据之前初始化视图控制器。

如何创建完成块:

在 iOS 中创建我自己的完成块

于 2013-07-25T18:49:54.257 回答
0

我认为您可以先不加载 UIViewController,直到您完成所有数据。或者,看起来您正在使用 tableViewController,因此只需在打开它时将行数设置为零,然后当数据到达时,将行数更改为您下载的正确数字。

更新:实际上,如果您在此行收到该错误:

 cell = [nib objectAtIndex:0];

然后看起来 nib 文件有问题 - 不存在,名称错误,名称大写,等等。

于 2013-07-25T18:50:36.813 回答
0

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section看起来像什么?

你应该返回count你的listOfProducts.

完成块完成后,您需要调用reloadDatatableview。您需要将一个指针传递给您的 tableview,或者将一个委托协议添加到您现有的代码中以使其工作。

编辑

添加一个委托协议以允许您的完成块告诉 tableview 重新加载。像这样的东西:

产品数据.h

@protocol ProductDataDelegate;

@interface ProductData
@property (nonatomic, weak) id<ProductDataDelegate> delegate;
...
@end

@protocol ProductDataDelegate <NSObject>
-(void)didFinishLoading;
@end

产品数据.m

在完成块的末尾,一旦加载完成:

if (self.delegate != nil)
{
    [self.delegate didFinishLoading];
}

StoreController.m

将委托设置为指向您的StoreController对象。

viewDidLoad()
...
_productData = [ProductData ProductData];
_productData.delegate = self;
...

并添加要重新加载的代码,假设 StoreController 是UITableViewController.

-(void)didFinishLoading()
{
    [self reloadData];
}

应该是这样!

于 2013-07-25T18:55:09.923 回答