-
-
Save jamesyu/ba03c1a550f14f88f95d to your computer and use it in GitHub Desktop.
// | |
// This is the template PFQueryTableViewController subclass file. Use it to customize your own subclass. | |
// | |
#import <UIKit/UIKit.h> | |
#import "Parse/Parse.h" | |
@interface MyPFQueryTableViewController : PFQueryTableViewController | |
@end | |
@implementation MyPFQueryTableViewController | |
- (id)initWithStyle:(UITableViewStyle)style { | |
self = [super initWithStyle:style]; | |
if (self) { | |
// Custom the table | |
// The className to query on | |
self.className = @"Foo"; | |
// The key of the PFObject to display in the label of the default cell style | |
self.textKey = @"text"; | |
// Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style | |
// self.imageKey = @"image"; | |
// Whether the built-in pull-to-refresh is enabled | |
self.pullToRefreshEnabled = YES; | |
// Whether the built-in pagination is enabled | |
self.paginationEnabled = YES; | |
// The number of objects to show per page | |
self.objectsPerPage = 25; | |
} | |
return self; | |
} | |
- (void)didReceiveMemoryWarning { | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
#pragma mark - UIViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Uncomment the following line to preserve selection between presentations. | |
// self.clearsSelectionOnViewWillAppear = NO; | |
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. | |
// self.navigationItem.rightBarButtonItem = self.editButtonItem; | |
} | |
- (void)viewDidUnload { | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
} | |
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
} | |
- (void)viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:animated]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated { | |
[super viewWillDisappear:animated]; | |
} | |
- (void)viewDidDisappear:(BOOL)animated { | |
[super viewDidDisappear:animated]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
// Return YES for supported orientations | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
#pragma mark - PFQueryTableViewController | |
- (void)objectsWillLoad { | |
[super objectsWillLoad]; | |
// This method is called before a PFQuery is fired to get more objects | |
} | |
- (void)objectsDidLoad:(NSError *)error { | |
[super objectsDidLoad:error]; | |
// This method is called every time objects are loaded from Parse via the PFQuery | |
} | |
/* | |
// Override to customize what kind of query to perform on the class. The default is to query for | |
// all objects ordered by createdAt descending. | |
- (PFQuery *)queryForTable { | |
PFQuery *query = [PFQuery queryWithClassName:self.className]; | |
// If Pull To Refresh is enabled, query against the network by default. | |
if (self.pullToRefreshEnabled) { | |
query.cachePolicy = kPFCachePolicyNetworkOnly; | |
} | |
// If no objects are loaded in memory, we look to the cache first to fill the table | |
// and then subsequently do a query against the network. | |
if (self.objects.count == 0) { | |
query.cachePolicy = kPFCachePolicyCacheThenNetwork; | |
} | |
[query orderByDescending:@"createdAt"]; | |
return query; | |
} | |
*/ | |
/* | |
// Override to customize the look of a cell representing an object. The default is to display | |
// a UITableViewCellStyleDefault style cell with the label being the textKey in the object, | |
// and the imageView being the imageKey in the object. | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { | |
static NSString *CellIdentifier = @"Cell"; | |
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
} | |
// Configure the cell | |
cell.textLabel.text = [object objectForKey:self.textKey]; | |
cell.imageView.file = [object objectForKey:self.imageKey]; | |
return cell; | |
} | |
*/ | |
/* | |
// Override if you need to change the ordering of objects in the table. | |
- (PFObject *)objectAtIndex:(NSIndexPath *)indexPath { | |
return [self.objects objectAtIndex:indexPath.row]; | |
} | |
*/ | |
/* | |
// Override to customize the look of the cell that allows the user to load the next page of objects. | |
// The default implementation is a UITableViewCellStyleDefault cell with simple labels. | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"NextPage"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
} | |
cell.selectionStyle = UITableViewCellSelectionStyleNone; | |
cell.textLabel.text = @"Load more..."; | |
return cell; | |
} | |
*/ | |
#pragma mark - UITableViewDataSource | |
/* | |
// Override to support conditional editing of the table view. | |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { | |
// Return NO if you do not want the specified item to be editable. | |
return YES; | |
} | |
*/ | |
/* | |
// Override to support editing the table view. | |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { | |
if (editingStyle == UITableViewCellEditingStyleDelete) { | |
// Delete the object from Parse and reload the table view | |
} else if (editingStyle == UITableViewCellEditingStyleInsert) { | |
// Create a new instance of the appropriate class, and save it to Parse | |
} | |
} | |
*/ | |
/* | |
// Override to support rearranging the table view. | |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { | |
} | |
*/ | |
/* | |
// Override to support conditional rearranging of the table view. | |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { | |
// Return NO if you do not want the item to be re-orderable. | |
return YES; | |
} | |
*/ | |
#pragma mark - UITableViewDelegate | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
[super tableView:tableView didSelectRowAtIndexPath:indexPath]; | |
} | |
@end |
Hi! Is it possible to use this class to control a UITableView (not fullscreen table view dragged from Objects Library to an existing ViewController)?
I am using storyboard and I got a similar exception saying that the loaded viewcontroller get something that is not UITableViewController
. I checked the storyboard and in the Interface Builder I deleted the view under the ViewController (the IB created it in the first time), and It works for me. Guessing it's that view masked the UITableView that comes along with PFQueryTableViewController. Maybe you might also want to check on that.
I'm trying to set the tableviewcell's separate color
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
but it didn't work.
One thing that needs made easier is the ability to create DetailViewControllers using storyboards and Parse. Currently I have it working but I had to include a lot of extra code that shouldn't be required.
Noob question here.
@crabacus mentioned, "As far as using initWithStyle or initWithCoder, neither of those are really necessary with a Storyboard implementation".
Everything is working fine with initWithCoder but when I remove the init method to use the Storyboard it crashes.
@crabacus also mentions to change the view controller's class to PFQueryTableViewController but in MasterViewController.h I have it sub-classed as so: @interface MasterViewController : PFQueryTableViewController and according to https://parse.com/tutorials/geolocations (section 2) they don't change the custom class in Identity Inspector when it's already set as a sub-class.
Any help much appreciated.
The className
property has been changed to parseClassName
property.
👍 @bjackson true that.
- (PFObject *)objectAtIndex:(NSIndexPath *)indexPath
should be
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
Hello, please update line 20 and 106 from:
// The className to query on
self.className = @"Foo";
to:
// The className to query on
self.parseClassName = @"Foo";
talking about efficient code. this is awesome template!
very good help
Hi,
I'm new to Parse and have this request. I want to build tho simple app in which I used Navigation controller ( so that I can go forward and backward from the main view) and has two buttons in the first view that will appear for the user, and each button will take the user to a different view. I dragged and dropped two view controllers, created a class named "CommentsViewController" which is a subclass of PFQueryTableViewController and assigned it to one of the two view controllers. Finally, I press on one button in the main view ctrl+click and drag to that view controller that has the above class assigned to it and clicked on push. Unfortunalty my app crashes when I want to go this view.
Any idea why??
please update line 20 and 106 from:
// The className to query on
self.className = @"Foo";
to:
// The className to query on
self.parseClassName = @"Foo";
Also, about storyboards, try commonInit
pattern.
- (void)commonInit
{
self.parseClassName = @"Foo";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 25;
self.loadingViewEnabled = NO;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (!self) return nil;
[self commonInit];
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (!self) return nil;
[self commonInit];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) return nil;
[self commonInit];
return self;
}
When deleting in
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
I always have to reload the complete data set using [self loadObjects]
. This seems wasteful and I'd love to see support to delete/reload single rows such that it can be used in combination with
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]
Are there plans for (or is there already) a similar sample subclass template file in Swift?
Any idea where I cold find this in SWIFT?
I agree with herveg, we would really appreciate the template written in Swift. Now its more pain than gain ...
Really appreciate it if you can have a swift version of this.
+1 for a modern Swift implementation.
What's the best way to reload the TableView and re-run the query programmatically(not using an interface control or gesture)? I think [self.tableView reloadData]; reloads the table view with the original query data not the refreshed data. Please help, Thanks!
EDIT: [self loadObjects];
Seems to do the task. Thanks!
Swift++
Here's one that supports Storyboards, customizing the default cells, changes the row height and adds a sort to the generated query written in Swift. Doesn't include everything as listed above but should get some of you guys started.
import UIKit
import Parse
import ParseUI
class ShipmentListViewController: PFQueryTableViewController {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// The className to query on
self.parseClassName = "ClassName"
// The key of the PFObject to display in the label of the default cell style
self.textKey = "text"
// Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style
self.imageKey = "image"
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = true
// Whether the built-in pagination is enabled
self.paginationEnabled = true
// The number of objects to show per page
self.objectsPerPage = 25
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = 54.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func queryForTable() -> PFQuery {
let query = super.queryForTable()
query.orderByAscending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath, object: object)
// Configure the cell
cell?.imageView?.contentMode = .ScaleAspectFill
cell?.imageView?.clipsToBounds = true
cell?.accessoryType = .DisclosureIndicator
return cell
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
For the ones that want to do the same in Swift, you can check the examples provided by Parse: https://github.com/ParsePlatform/ParseUI-iOS/tree/master/ParseUIDemo/Swift/CustomViewControllers/QueryTableViewController
Hi, I'm trying to implement a PFQueryTableViewController with storyboards, and I keep getting this error:
2012-07-11 01:31:01.360 ParseStarterProject[35323:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The nib file did not specify a UITableView as its view of PFQueryTableViewController.'
I've tried to add the "User Defined Runtime Attributes" in Interface Builder, as suggested above. But that hasn't worked.
I'm using Xcode 4.3.2 and I have the latest Parse SDK: v1.0.56 (downloaded this past week).
I've tried creating both a UITableViewController and a UIViewController (as the latest PFQueryTableViewController no longer subclasses UITableViewController by UIViewController instead). But that doesn't work either.