问题
I have a 'Game' table in my parse database with the following relevant columns:
- isOver (Boolean)
- whoseTurn (Relation:_User) (Games can have multiple players, so it can be more than one person's turn at the same time)
- players (Relation:_User)
I need to query this table and display the results in 3 separate sections of the tableview:
- 'My Turn'
- 'Their Turn'
- 'Game Over'
I was thinking of using the following query coupled with some of the code in the answer given here: https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"players" equalTo:[PFUser currentUser]];
//BREAK UP INTO SECTIONS
[query orderByDescending:@"isOver"];
[query addDescendingOrder:@"whoseTurn"];
How can I display the results in 3 different tableview sections?
回答1:
The question contains two challenges, and that's one too many for the PFQueryTableVC
. The post you site makes evident that you can get sections with a little extra overriding, but what's not covered there is that your model depends on related objects (users) via a PFRelation.
That extra requirement rules out the PFQueryTableVC because it assumes your table can be fetched in a single query, but yours depends on fetching those two relations columns.
This is going to force you to roll your own with a plain old UIViewController
that has a UITableView
as its view's subview. (Notice I don't recommend UITableViewController
here... just like PFQueryTableVC
, it often generates more effort than convenience, though you could probably get away with it here).
The gist of how to do it is this: in succession, query the games table, then it's whoseTurn
relation, then it's players
relation. With that data, you'll be able to segregate the found games into three arrays, corresponding exactly to your table view sections: gamesThatAreOver
, gamesThatAreUsersTurn
, gamesThatAreOtherUsersTurn
.
You can put those arrays in another, call it model
. Tell your table view that you have self.model.count
sections (that will always be ==3
), and that the number of rows at an index path is self.model[indexPath.section].count
Your version of object at index path will give you a game, and will look like this: self.model[indexPath.section][indexPath.row];
PS - I think freeing yourself from the "convenience" (aka constraint) of the parse provided class is going to be a net positive anyway.
来源:https://stackoverflow.com/questions/28422402/pfquerytableviewcontroller-with-3-sections