【问题标题】:Building a simple tableview using Parse PFQuery使用 Parse PFQuery 构建一个简单的 tableview
【发布时间】:2015-08-22 18:05:31
【问题描述】:

您好,刚开始使用 Parse,我正在尝试使用 Parse PFQuery 检索的数组中的数据加载一个简单的表视图控制器。虽然我可以 nslog 加载视图中的“类别”数组,但当代码到达 numberOfRowsInSection 时,该数组似乎已重置为零。 对此的任何帮助将不胜感激。 顺便说一句,我确实尝试过将代码加载到带有文字的数组中,并且表格显示正常没有问题。 代码如下:

@implementation DisplayCategoriesTVC

NSArray *categories;

- (void)viewDidLoad {
    [super viewDidLoad];

    // CODE TO RETRIEVE CONTENTS OF THE PARSE CATEGORIES CLASS

    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    //    [query whereKey:@"Sequence" > @1];
    [query findObjectsInBackgroundWithBlock:^(NSArray *categories, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %lu categories.", (unsigned long)categories.count);
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
       return [categories count];
}

我的具体问题是,为什么在 numberOfRowsInSection 类别数组显示 nil 值?

我的具体问题是,为什么类别数组现在显示为 nil,我可以做些什么来保留 PFQuery 加载的值并在我的其他方法中使用它们?

【问题讨论】:

    标签: ios objective-c uitableview parse-platform pfquery


    【解决方案1】:

    您正在后台线程上执行某些操作:

    findObjectsInBackground:
    

    既然你是新人,这意味着什么?

    What's the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

    那么当你的数据最终从后台任务聚合时,你如何reload the tableView

    您只需重新加载 tableView,但我们需要在主线程上执行此操作,因为 UI 更新发生在那里:

    [self.tableView reloadData];
    

    欲了解更多信息,请参阅:

    iPhone - Grand Central Dispatch main thread

    如此彻底:

    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    //    [query whereKey:@"Sequence" > @1];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %lu categories.", (unsigned long)categories.count);
            self.categories = objects;
            //Since this is a UI update we need to perform this on the main thread:
            dispatch_async(dispatch_get_main_queue(), ^{
              [self.tableView reloadData];
            });
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    

    您的查询在更新 UI 之前已完成其任务,因为它发生在后台线程上,因此您需要在完成时告诉您的 UI 组件。

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多