如何使用关系查询解析iOS以填充UITableView?

时间:2015-05-25 14:08:12

标签: ios objective-c uitableview parse-platform

我正在开发一个社交应用,并且在从选定单元格中创建的解析填充对象时遇到一些问题。

主屏幕是一个UITableViewController,填充存储在Parse中的对象数组,当用户点击一个单元格时,将推送一个新的场景DetailViewController,它会在视图中显示所选单元格中的对象。

接下来,我在DetailViewController中创建了一个UIButton,用于将对象添加到名为“replies”的新类中,并且还添加到DetailViewController场景中,使用来自“replies”类中这些对象的新数组填充TableView。

我想只检索在所选单元格上创建的对象:(目前一切正常,除了在DetailViewController场景上,TableView将显示在“回复”类中创建的所有对象,无论我点击哪个单元格。

我想知道如何从所选单元格存储一个数组,以便我可以填充在所选单元格中创建的对象...我相信我可以通过解析检索解析这个问题解决这个问题方法。

会非常感谢任何帮助。

.h文件

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <ParseUI/ParseUI.h>
#import "GroundTableViewCell.h"
#import "TimelineTableViewController.h"

@interface DetailViewController : UIViewController<
UITableViewDataSource,
UITableViewDelegate,
NSObject>
{
}

@property (strong, nonatomic) IBOutlet UITableView *detailTableView;
@property (strong, nonatomic) IBOutlet UITextView *TextField;
@property (nonatomic, strong) PFObject *groUnds;
@property (strong, nonatomic) IBOutlet UITextView *replyTextView;
- (IBAction)sendReply:(id)sender;

@end

.m文件

#import "DetailViewController.h"
#import <Parse/Parse.h>
#import "GroundTableViewCell.h"
#import "TimelineTableViewController.h"

@interface DetailViewController ()
@property(strong)NSMutableArray* repliesMutableArray;

@end

@implementation DetailViewController

@synthesize groUnds;
@synthesize TextField;
@synthesize detailTableView;
@synthesize repliesMutableArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    PFUser *currentUser = [PFUser currentUser];
    if (currentUser) {
        // do stuff with the user
    } else {
        // show the signup or login screen
    }

    // Do any additional setup after loading the view.
//      [self performSelector:@selector(retrieveFromParse)];
//    repliesArray = [[NSArray alloc] initWithObjects:@"comment", nil];

       [self performSelector:@selector(retrieveFromParse)];

    // Set the Label text with the selected detail.
    self.TextField.text = [self.groUnds objectForKey:@"comment"];
}

- (void) retrieveFromParse {
    PFQuery *retrieveReplies = [PFQuery queryWithClassName:@"Replies"];

  // This is the part where im not really sure how to query...
 // if uncommented won't show any objects at all now shows all the objects from the "Replies class" -->
  //  [retrieveReplies whereKey:@"comment" equalTo:groUnds];
    [retrieveReplies orderByDescending:@"createdAt"];

    [retrieveReplies findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            repliesMutableArray = [[NSMutableArray alloc] initWithArray:objects];
        }
        [detailTableView reloadData];
    }];
}

//*********************Setup table of folder names ************************
//get number of sections in tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

//get number of rows by counting number of folders
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [repliesMutableArray count ];
}

//setup cells in tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"replyCell";
    GroundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject *tempObject = [repliesMutableArray objectAtIndex:indexPath.row];
    cell.cellTitle.text = [tempObject objectForKey:@"commentReplies"];

    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 //reply button
- (IBAction)sendReply:(id)sender {
    //1
    //Add the image to the object, and add the comment and the user
    PFObject *Reply = [PFObject objectWithClassName:@"Replies"];
    [Reply setObject:[PFUser currentUser].username forKey:@"user"];
    [Reply setObject:self.replyTextView.text forKey:@"commentReplies"];
    //2
    [Reply saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        //3
        if (succeeded){
            //Go back to the wall
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            NSString *errorString = [[error userInfo] objectForKey:@"error"];
            UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [errorAlertView show];
        }
    }];
}

@end

1 个答案:

答案 0 :(得分:0)

<强>概述

在视图控制器之间传递信息的基本结构是,当您在主视图控制器(MVC)中时,需要设置要在详细视图控制器(DVC)中显示的数据。因此,在选择单元格(didSelectRowAtIndexPath)时调用的MVC中的委托方法,您可以获取特定于所选单元格的数据并将其放入变量中。在prepareForSegue ...方法内部,您可以将该数据设置为dvc上代表您的tableview数据的变量。

在视图控制器之间传递数据

要了解如何在视图控制器之间正确传递数据,请在SO上查看此highly rated question的答案。

您可以将此视为推动而非拉动。在显示视图之前,应将数据添加到DVC上的属性中。有许多方法可以使用NSPredicates,SubQueries,Related Queries或Queries on Array Values来限制数据:

查询数组值

对于具有数组类型的键,您可以找到键的数组值包含2的对象:

// Find objects where the array in arrayKey contains 2.
// Using PFQuery
[query whereKey:@"arrayKey" equalTo:@2];

// Or using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"2 IN arrayKey"];
PFQuery *query = [PFQuery queryWithClassName:@"MyClass" predicate:predicate];

more Parse Queries

请注意,您还可以使用Apple API解决方案来优化添加到DVC属性中的数据,从而解决这一细化数据的问题。你提到你是新手,所以请确保你已经阅读并完成了下面的入门链接教程。这很重要,因为它显示了Apple在iOS中处理数据移动的范例。

使用入门

如果解释没有意义,请查看getting started guide。在这个答案的背景下,有一些关键的概念无法掩饰或容易解释。

提议的决议

将所选单元格的indexPath.row传递给详细视图控制器,以便可以将数组的结果限制为该单元格。如果indexPath行对此目的没用,那么只需传递一个值,该值将结果限制为所选单元格中的唯一值。在tableview委托上使用委托方法didSelectRowAtIndexPath来设置传递给在详细信息场景上管理基础tableview的对象的相关数据值。您需要识别适合于要在DVC中显示的数据的“where子句”,然后使用新查询或nspredicate来限制数据数组的结果。搜索SO以获取每个概念的示例。

希望这些提示可以帮助您找到最终答案。搜索SO将为您提供示例,然后您需要应用于您的特定情况。但是,我真的建议您退一步,以确保您了解如何正确地在视图控制器之间传递数据。