【问题标题】:Get an array from Parse query [duplicate]从 Parse 查询中获取一个数组 [重复]
【发布时间】:2015-02-09 17:12:02
【问题描述】:

我正在使用Parse 创建此表视图,并试图弄清楚如何将Parse 表数据放入数组中,因此我可以将其传递给WatchKit InterfaceController 以显示完全相同东西?

所以我想在 WatchKit 界面中准确显示 iPhone 界面中显示的内容。

这是我所拥有的,如果我可以添加任何有用的内容,请告诉我:

TableVC.m:

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) {
        self.parseClassName = @"na";
        self.textKey = @"dateTime";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = NO;
    }
    return self;
}
- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    static NSString *simpleTableIdentifier = @"RecipeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    UILabel *homeLabel = (UILabel*) [cell viewWithTag:101];
    homeLabel.text = [object objectForKey:@"test"];

    UILabel *dateLabel = (UILabel*) [cell viewWithTag:102];
    dateLabel.text = [object objectForKey:@"dateTime"];

    return cell;
}

Parse data:

TableVC.m:

我已经设置了基本的 WatchKit 文件和 Storyboard。我硬编码了一个数组来测试它是否正常工作。但是现在我只需要从Parse 获取数据到那里,并且不确定我是否需要进行查询然后将其变成公共array

编辑

这是我的查询:

PFQuery *query2 = [PFQuery queryWithClassName:@"nba"];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Objects 2: %@", objects);
        }
    } else {
        // Log details of the failure
        NSLog(@"Error 2: %@ %@", error, [error userInfo]);
    }
}];

这是我的 NSLog:

NSLog(@"Objects 2: %@", objects);

控制台:

2015-02-09 21:06:30.845 SimpleTable[8373:1284663] Objects 2: (
    "<na: 0x7ff3f8e40880, objectId: cOrjeAmwJh, localId: (null)> {\n    away = Cav;\n    date = \"04/19/2015\";\n    dateTime = \"April 19, 2015, 16:00\";\n    gNumber = 1;\n    home = Bul;\n    matup = \"Ca\";\n    ro = \"Ro \";\n    test = \"Test 2\";\n    tv = T;\n}",

【问题讨论】:

    标签: ios arrays uitableview parse-platform watchkit


    【解决方案1】:

    如果您需要该数组,请在一个方法queryForTable 方法之外异步获取它,像这样获取它:

    PFQuery *query = [self queryForTable];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // objects is the array for this table
            NSMutableArray *array = [@[] mutableCopy];
            for (PFObject *object in objects) {
                NSLog(@"we got an object with dateTime = %@", [object objectForKey:@"dateTime"]);
                [array addObject:[object objectForKey:@"dateTime"]];
                // you can prove this with any of your keys: away, number, home, mat up, etc.
            }
        }
    }];
    

    【讨论】:

    • 感谢您的回答,这就是我一直在尝试的事情。他们在文档中为类似的事情奠定了基础,但我无法弄清楚// objects is the array for this table 部分的内容。当我使用 NSLog 记录时,我似乎只是在获取指针,而不是数组。我在原始问题的末尾发布了上面的代码。有什么想法吗?
    • 它看起来像来自 parse 的健康响应,如果您阅读乱码,您可以看到诸如 dateTime、away、gNumber、mat up 等键的键值对。请参阅编辑以证明代码你有一个 pfobjects 数组。
    • 对,我有点像这样的东西,也尝试了一个版本,但我很困惑,因为它似乎只是在迭代它并吐出所有单独的对象(如你所见你的代码是NSLogs 一个dateTime,对吗?),而不是给我一个array。您知道如何获得dateTimes 中的array 吗?就像您将添加一两行代码到您已经拥有的代码中,将所有这些 dateTimes 变成 array?谢谢!我快到了!
    • @SRMR,见编辑。数组将使用所有日期进行初始化。
    • 太棒了。然后我是否需要公开该数组以便WatchKit InterfaceController.m 可以访问它?
    【解决方案2】:

    如果您只想将对象传递到数组中,您可以这样做或以下方式的变体:

    - (PFQuery *)queryForTable
    {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    
        self.someArray = [query findObjects];
    
    return query;
    }
    

    参考

    【讨论】:

    • 我认为这是一个非常糟糕的主意。您所做的是让查询在 PFQueryTableVC 超类调用 queryForTable 方法时同步运行,这谁知道何时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 2015-11-13
    • 2021-08-01
    • 1970-01-01
    • 2023-03-18
    • 2021-04-19
    相关资源
    最近更新 更多