簡體   English   中英

如何使用關系查詢解析iOS填充UITableView?

[英]How to Use Relational Queries Parse iOS to populate a UITableView?

我正在開發一個社交應用程序,並且在從選定單元格中創建的解析中填充對象時遇到一些問題。

主屏幕是一個UITableViewController,用於填充存儲在Parse中的對象的數組,當用戶點擊一個單元格時,將推送一個新的場景DetailViewController,該視圖將在視圖中顯示所選單元格中的對象。

接下來,我在DetailViewController中創建了一個UIButton,用於將對象添加到名為“ replies”的新類中,並且還將TableView添加到DetailViewController場景中,該TableView使用來自“ replies”類中這些對象的新數組填充。

我只想檢索在選定單元格上創建的對象:(目前,除了在DetailViewController場景上,一切正常之外,無論我點擊哪個單元格,TableView都將顯示在“ replies”類中創建的所有對象。

我不知道如何存儲選定單元格中的數組,以便可以填充在該選定單元格中創建的對象...我相信我可以通過在parse from parse方法中進行解析的關系查詢來解決此問題。

會非常感謝您的幫助。

.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

概觀

在視圖控制器之間傳遞信息的基本結構是,當您位於主視圖控制器(MVC)中時,需要設置要在明細視圖控制器(DVC)中顯示的數據。 因此,在選擇單元格(didSelectRowAtIndexPath)時調用的MVC中的委托方法上,可以獲得特定於所選單元格的數據並將其放入變量中。 在prepareForSegue ...方法內部,可以將數據設置為dvc上代表表視圖數據的變量。

在視圖控制器之間傳遞數據

要了解如何在視圖控制器之間正確傳遞數據,請查看對SO的高度評價的問題的答案。

您可以將其視為推動而非拉動。 在顯示視圖之前,應將數據添加到DVC的屬性中。 使用NSPredicates,子查詢,相關查詢或數組值查詢有多種方法來限制數據:

數組值查詢

對於具有數組類型的鍵,可以通過以下方式找到鍵的數組值包含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];

更多解析查詢

請記住,您還可以使用Apple API解決方案來優化添加到DVC屬性中的數據,從而解決優化數據的問題。 您提到您是新手,因此請確保您已閱讀並完成下面的入門鏈接上的教程。 這很重要,因為它顯示了Apple處理iOS中數據移動的范例。

入門

如果解釋不合理,請查閱入門指南 在此答案的上下文中,有些關鍵概念無法掩蓋或輕易解釋。

擬議決議

將所選單元格的indexPath.row傳遞到詳細信息視圖控制器,以便可以將數組的結果限制為該單元格。 如果indexPath行對此沒有用,則只需傳遞一個值,該值會將結果限制為所選單元格中唯一的值。 在表視圖委托上使用委托方法didSelectRowAtIndexPath來設置一個相關的數據值,該數據值將傳遞給管理詳細場景中基礎表視圖的對象。 您將要確定一個與要在DVC中顯示的數據相對應的“ where-clause”,然后使用新查詢或nspredicate來限制數據數組中的結果。 在SO中搜索每個概念的示例。

希望這些技巧將幫助您找到最終的答案。 搜索SO將為您提供示例,然后您需要將這些示例應用於您的特定情況。 但是,我確實建議您退一步,以確保您了解如何正確在視圖控制器之間傳遞數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM