Ios 初始化视图控制器后的委托集使用解析方法QueryForTableView创建问题
我正在使用PFQueryTableViewController,它要求在调用QueryForTableView之前将某个PFUser传递给它。为了解决这个问题,我在PFQueryTableViewController中创建了一个协议,该协议具有一个名为userForProfileTableView的必需方法Ios 初始化视图控制器后的委托集使用解析方法QueryForTableView创建问题,ios,objective-c,xcode,parse-platform,Ios,Objective C,Xcode,Parse Platform,我正在使用PFQueryTableViewController,它要求在调用QueryForTableView之前将某个PFUser传递给它。为了解决这个问题,我在PFQueryTableViewController中创建了一个协议,该协议具有一个名为userForProfileTableView的必需方法 @protocol profileTableViewDataSource <NSObject> @required -(PFUser *)userForProfileTabl
@protocol profileTableViewDataSource <NSObject>
@required
-(PFUser *)userForProfileTableView;
@end
包含协议的视图控制器,正在检索PFUser以作为Tableview数据的基础
h
在上一个视图控制器上尝试为queryForTable方法设置对象时,我遇到了类似的问题。我提出的解决方案是创建一个自定义初始值设定项来设置对象,然后调用超级初始值设定项。 e、 g
我知道这不是一个很好的解决方案,但这是唯一一种使用查询方便的方法来使用我的对象进行查询,而不必在ViewDidDisplay中重新运行查询的方法。感谢您的回答,这将解决问题,但我使用的是故事板,它会在后台初始化视图控制器。似乎这个解析类是在考虑xib的情况下构建的。我怎么能自己调用初始值设定项而不是编译器为我编写它呢?好的,我找到了答案。出于某种原因,我不得不把项目干净利落。出于某种原因,这使得该计划得以实施。我不知道为什么。
#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
//
// 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
//
// 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
- (id)initWithParent:(PFObject *)parent
{
self.parent = parent;
self = [self initWithStyle:UITableViewStylePlain];
if (self) {
...
}
return self;
}