我正在使用PFQueryTableViewController,它需要在调用QueryForTableView之前将某个PFUser传递给它。为了解决这个问题,我在PFQueryTableViewController中创建了一个协议,它有一个名为userForProfileTableView的必需方法。
@protocol profileTableViewDataSource <NSObject>
@required
-(PFUser *)userForProfileTableView;
@end
上一个视图控制器使用此方法设置PFUser。但是,以前的视图控制器仅将自身设置为prepareForSegue:sender:completion中的dataSource。调用此方法时,已调用queryForTableView和initWithCoder方法,这意味着PFQueryTableViewClass没有PFObject可供使用。
如何在初始化后直接获得对PFQueryTableViewController的引用,或者在调用queryForTableView之前是否可以通过另一种方式传递PFUser?任何帮助/想法将非常感激。
提前致谢!
代码:
两者都是PFQueryTableViewController的子类
第一个视图控制器,它是数据源:
.h(不重要)
的.m
#import "FeedQueryTableViewController.h"
#import "FeedTableViewCell.h"
@interface FeedQueryTableViewController ()
@end
@implementation FeedQueryTableViewController{
PFUser * userToPass;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.parseClassName = @"Question";
}
return self;
}
-(PFQuery *)queryForTable{
//First query for followed users
PFQuery * query = [PFQuery queryWithClassName:@"Activity"];
[query whereKey:@"fromUser" equalTo:[PFUser currentUser]];
[query whereKey:@"type" equalTo:@"follow"];
[query includeKey:@"toUser"];
[query includeKey:@"fromUser"];
//Second query for questions
PFQuery * questionQuery = [PFQuery queryWithClassName:@"Question"];
[questionQuery whereKey:@"User" matchesKey:@"toUser" inQuery:query];
[questionQuery includeKey:@"User"];
return questionQuery;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
FeedTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[FeedTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
//Set values of cell
[cell.usernameButton setTitle:[[object objectForKey:@"User"]username] forState:UIControlStateNormal];
[cell.usernameButton addTarget:self action:@selector(userButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cell.usernameButton.tag = indexPath.row;
cell.questionTextView.text = [object objectForKey:@"Question"];
return cell;
}
-(PFUser *)userForProfileTableView{
//This happens before the other class
if (userToPass != nil){
NSLog(@"%@", userToPass.username);
return userToPass;}
else{
return userToPass;
}
}
-(void)userButtonPressed:(UIButton *)sender {
//Type checking is sketchy here and could lead to potential crashes
PFObject * object = [[[self queryForTable]findObjects] objectAtIndex:sender.tag];
userToPass = [object objectForKey:@"User"];
[self performSegueWithIdentifier:@"userProfile" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ProfileTableViewController * ptvc = segue.destinationViewController;
ptvc.dataSource = self;
}
@end
View Controller包含协议,并且正在检索PFUser以基于Tableview数据
·H
//
// ProfileTableViewController.h
// qSocialNetwork
//
// Created by Sam Kirkiles on 7/29/14.
// Copyright (c) 2014 Sam Kirkiles. All rights reserved.
//
#import <Parse/Parse.h>
@protocol profileTableViewDataSource <NSObject>
@required
-(PFUser *)userForProfileTableView;
@end
@interface ProfileTableViewController : PFQueryTableViewController
@property (weak, nonatomic) IBOutlet UILabel *profileNameLabel;
@property (strong, nonatomic) PFUser * userToDisplay;
@property (weak) id <profileTableViewDataSource> dataSource;
@end
的.m
//
// ProfileTableViewController.m
// qSocialNetwork
//
// Created by Sam Kirkiles on 7/29/14.
// Copyright (c) 2014 Sam Kirkiles. All rights reserved.
//
#import "ProfileTableViewController.h"
@interface ProfileTableViewController ()
@end
@implementation ProfileTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.dataSource userForProfileTableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
self.parseClassName = @"Activity";
}
return self;
}
-(void)objectsWillLoad{
[super objectsWillLoad];
self.userToDisplay = [self.dataSource userForProfileTableView];
if (self.userToDisplay != nil) {
NSLog(@"sdfsfd");
[self queryForTable];
//A user has been passed to this view controller
}else{
//User the current user as a backup
//This feature will be added later
}
}
-(PFQuery *)queryForTable{
if (self.userToDisplay != nil){
PFQuery * query = [PFQuery queryWithClassName:@"Question"];
[query whereKey:@"User" equalTo:self.userToDisplay];
return query;
}
PFQuery *noUserQuery = [PFQuery queryWithClassName:@"Question"];
return noUserQuery;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [object objectForKey:@"Question"];
return cell;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
答案 0 :(得分:0)
尝试在前一个视图控制器上为queryForTable方法设置对象时遇到了类似的问题。我想出的解决方案是创建一个自定义初始化程序,设置对象,然后调用超级初始化程序。 e.g。
- (id)initWithParent:(PFObject *)parent
{
self.parent = parent;
self = [self initWithStyle:UITableViewStylePlain];
if (self) {
...
}
return self;
}
我知道这不是一个优雅的解决方案,但这是我可以让queryForTable方法使用我的对象进行查询而不必在viewDidAppear中重新运行查询的唯一方法。