[英]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.