[英]Parse Framework iOS: Query for header by date
情境
我有一个应用程序,允许用户将建议发布到PFQueryTableViewController上。 我正在使用parse.com框架,并且每个帖子都以附加的“日期” 00/00/00
和“时间” 00:00PM
。
我需要的
要在00/00/00
为每一天设置部分(“过帐”中的“日期”对象)-将00/00/00
显示为该部分的标题。
题
有谁知道如何设计这样的东西?
在旧的parse.com论坛上发布了类似以下内容的内容:
https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections
它用于运动,但是您可以轻松地对其进行修改以满足您的需求。
如果链接消失,这是代码:
SectionedQueryTableViewController.h
@interface SectionedQueryTableViewController : PFQueryTableViewController
@end
SectionedQueryTableViewController.m
#import "SectionedQueryTableViewController.h"
@interface SectionedQueryTableViewController()
@property (nonatomic, retain) NSMutableDictionary *sections;
@property (nonatomic, retain) NSMutableDictionary *sectionToSportTypeMap;
@end
@implementation SectionedQueryTableViewController
@synthesize sections = _sections;
@synthesize sectionToSportTypeMap = _sectionToSportTypeMap;
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
self.className = @"Athlete";
self.textKey = @"name";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 25;
self.sections = [NSMutableDictionary dictionary];
self.sectionToSportTypeMap = [NSMutableDictionary dictionary];
}
return self;
}
#pragma mark - PFQueryTableViewController
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
// This method is called every time objects are loaded from Parse via the PFQuery
[self.sections removeAllObjects];
[self.sectionToSportTypeMap removeAllObjects];
NSInteger section = 0;
NSInteger rowIndex = 0;
for (PFObject *object in self.objects) {
NSString *sportType = [object objectForKey:@"sportType"];
NSMutableArray *objectsInSection = [self.sections objectForKey:sportType];
if (!objectsInSection) {
objectsInSection = [NSMutableArray array];
// this is the first time we see this sportType - increment the section index
[self.sectionToSportTypeMap setObject:sportType forKey:[NSNumber numberWithInt:section++]];
}
[objectsInSection addObject:[NSNumber numberWithInt:rowIndex++]];
[self.sections setObject:objectsInSection forKey:sportType];
}
}
- (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;
}
// Order by sport type
[query orderByAscending:@"sportType"];
return query;
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *sportType = [self sportTypeForSection:indexPath.section];
NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
return [self.objects objectAtIndex:[rowIndex intValue]];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sections.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *sportType = [self sportTypeForSection:section];
NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
return rowIndecesInSection.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sportType = [self sportTypeForSection:section];
return sportType;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
PFObject *selectedObject = [self objectAtIndexPath:indexPath];
}
#pragma mark - ()
- (NSString *)sportTypeForSection:(NSInteger)section {
return [self.sectionToSportTypeMap objectForKey:[NSNumber numberWithInt:section]];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.