如何将检索到的对象从 parse.com 传递到 NSMutableArray 以与 UITableView 一起使用?

Posted

技术标签:

【中文标题】如何将检索到的对象从 parse.com 传递到 NSMutableArray 以与 UITableView 一起使用?【英文标题】:How do I pass retrieved objects from parse.com into an NSMutableArray to use with UITableView? 【发布时间】:2014-02-22 05:00:23 【问题描述】:

我刚刚完成了一个慈善应用程序,直到现在我使用临时用户来填充我的表格视图单元格。我将这些用户对象存储在 NSMutableArray (*people) 中。该应用程序运行良好,我正在尝试将我在 parse.com 中创建的对象添加到同一个人员数组中。

我尝试了很多不同的方法,但一直收到我粘贴在这篇文章底部的错误。

我如何使用从表单中获取的数据填充我的 parse.com 数据库:

PFObject *newPerson = [PFObject objectWithClassName:@"Person"];

    NSNumber *age = [NSNumber numberWithInt:[[self mPerson] ageAtDisappearance]];
    NSNumber *active = [NSNumber numberWithInt:[[self mPerson] active]];
    NSData* data = UIImageJPEGRepresentation([[self mPerson] image], 0.5f);
    PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:data];


    newPerson[@"name"] = [[self mPerson] name];
    newPerson[@"ageAtDisappearance"] = age;
    newPerson[@"since"] = [[self mPerson] mSince];
    newPerson[@"from"] = [[self mPerson] mFrom];
    [newPerson setObject:imageFile forKey:@"image"];
    newPerson[@"active"] = active;
    [newPerson saveInBackground];

我如何在本地填充表格的示例(此代码在下面的 viewDidLoad 方法中:

Person *person = [[Person alloc] init];
[person setName:@"Ben Huddle"];
[person setNotes:@"Some text here"];
[person setAgeAtDisappearance:18];
[person setSince:[NSDate date]];
[person setFrom:@"London, SW"];
[person setReferenceNumber:@"14-334544"];
UIImage *image = [UIImage imageNamed:@"ben.png"];
[person setImage:image];
[person setActive:1];
[people addObject:person];

viewDidLoad:

    people = [[NSMutableArray alloc] init];


    PFQuery *query = [PFQuery queryWithClassName:@"Person"];
   // [query whereKey:@"active" equalTo:@0];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) 

        if (!error) 
            for (PFObject *object in objects) 
                 NSLog(@"%@", object);
                PFObject *person = [object objectForKey:[object objectId]];
                [people addObject:person];
            

            dispatch_async(dispatch_get_main_queue(), ^ 
                [[self tableView] reloadData];
            );
         else 
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        
    ];

我的 UITableView:

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


    // Return the number of rows in the section.

    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
        return [searchResults count];

     else 
        return [people count];
    


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    Person *current;
    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
        current = [searchResults objectAtIndex:indexPath.row];
     else 
        current = [people objectAtIndex:[indexPath row]];
    

    [[cell textLabel] setText: [current name]];
    [[cell imageView] setImage: [current image]];
    [[cell detailTextLabel] setText: [current notes]];

    return cell;

错误:

2014-02-22 04:38:43.912 MPeople[28451:70b] <Person:SVeM3MnILW:(null)> 
    active = 0;
    ageAtDisappearance = 34;
    image = "<PFFile: 0xbe16880>";
    from = Eastham;
    since = "2014-02-22 00:00:00 +0000";
    name = "John Smith";

2014-02-22 04:38:43.914 MPeople[28451:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x027b85e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0253b8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0276abcc -[__NSArrayM insertObject:atIndex:] + 844
    3   CoreFoundation                      0x0276a870 -[__NSArrayM addObject:] + 64
    4   MPeople                       0x000102fc __42-[MPPeopleTableViewController viewDidLoad]_block_invoke + 620
    5   MPeople                       0x00062667 __40-[PFTask thenCallBackOnMainThreadAsync:]_block_invoke_2 + 241
    6   libdispatch.dylib                   0x02a527f8 _dispatch_call_block_and_release + 15
    7   libdispatch.dylib                   0x02a674b0 _dispatch_client_callout + 14
    8   libdispatch.dylib                   0x02a5575e _dispatch_main_queue_callback_4CF + 340
    9   CoreFoundation                      0x0281da5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    10  CoreFoundation                      0x0275e6bb __CFRunLoopRun + 1963
    11  CoreFoundation                      0x0275dac3 CFRunLoopRunSpecific + 467
    12  CoreFoundation                      0x0275d8db CFRunLoopRunInMode + 123
    13  GraphicsServices                    0x043399e2 GSEventRunModal + 192
    14  GraphicsServices                    0x04339809 GSEventRun + 104
    15  UIKit                               0x012a9d3b UIApplicationMain + 1225
    16  MPeople                             0x0000620d main + 141
    17  libdyld.dylib                       0x02cf970d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

感谢您的宝贵时间。 亲切的问候

【问题讨论】:

【参考方案1】:

在 viewDidLoad 中的查询中,您应该先将 PFObject 解析为 Person 对象,然后再将它们一一添加到您的人员数组中。以下行看起来很可疑,因为您将 PFObject 添加到 Persons 数组中:

PFObject *person = [object objectForKey:[object objectId]];
[people addObject:person];

相反,您应该将每个 PFObject 的值解析到您的 Person 对象中:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) 

    if (!error) 
        for (PFObject *object in objects) 
             NSLog(@"%@", object);

             Person *person = [[Person alloc] init];
             [person setName:[object objectForKey:@"name"]];
             // ... similarly, populate the rest of the Person properties from your PFObject 
             UIImage *image = [object objectForKey:@"image"];
             [person setImage:image];
             [people addObject:person];
        

        dispatch_async(dispatch_get_main_queue(), ^ 
            [[self tableView] reloadData];
        );
     else 
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    
]

【讨论】:

我真的不知道我怎么会错过它。除了图像之外,它现在正在工作。应该很容易解决谢谢。 不客气。快速检查似乎表明图像必须转换为 NSData,然后传递到 PFFile,然后再将其设置为 PFObject 的属性。见parse.com/tutorials/saving-images

以上是关于如何将检索到的对象从 parse.com 传递到 NSMutableArray 以与 UITableView 一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

从 Parse.com 流式传输音频 - 没有检索到数据?

如何将检索到的数据从 v-for 绑定到 Vuejs 中的数据对象?

如何将从 json 检索到的数据从一个视图传递到另一个视图? [复制]

从 Parse.com (Swift) 检索数据

从 Parse.com 检索数据到 ArrayList<Object>

如何从 Parse.com 检索数组