【问题标题】:Load data before UITableViewController will be shown在 UITableViewController 显示之前加载数据
【发布时间】:2013-07-25 18:38:35
【问题描述】:

我有一个应用程序,我必须从 Internet 获取信息。对 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]];
}

ProductData.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++;
             }
         }
     }];

}

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

【问题讨论】:

  • 异常名称、消息和行是什么?
  • 我只是更新我的问题
  • 你能做到吗?在下面查看我的更新答案。
  • 谢谢,我正在努力。我的数据有问题。很快会更新我的问题。现在我禁止这样做。

标签: ios uitableview objective-c-blocks


【解决方案1】:

你的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 是什么样的?

您应该返回您的listOfProducts 的count。

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

编辑

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

ProductData.h

@protocol ProductDataDelegate;

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

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

ProductData.m

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

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

StoreController.m

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

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

并添加重新加载的代码,假设StoreController是UITableViewController的子类。

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

应该是这样的!

【讨论】:

  • 哦,我认为这是我的错误! numberOfRows 错误,现在有 listOfProducts 的计数。但是表格显示为空,并且在显示表格后日志告诉我数据已加载,但我不知道何时必须重新加载表格或其他任何东西。
  • 查看我的编辑。这显示了如何创建一个委托协议来处理重新加载。它应该关闭,以防您以前从未创建过委托协议。
  • 这对我帮助很大!谢谢!
【解决方案2】:

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

如何创建完成块:

Create my own completion blocks in iOS

【讨论】:

    【解决方案3】:

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

    更新:实际上,如果您在这一行遇到该错误:

     cell = [nib objectAtIndex:0];
    

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

    【讨论】:

      【解决方案4】:

      另一种解决方案可以是:

      在 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++;
                   }
               }
           }];
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-29
        • 2017-10-29
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多