首先,它可能是一个新手问题,但我试图解决它一段时间而我不能。
使用[PFQueryTableViewController] [1],我希望有与日期相关的部分。
示例:本周,下周,很快。
关于在PFQueryTableViewController上添加部分的问题在[Parse topic] [2]中讨论。但这种方法与日期间隔无关,我未能使其适应我的目标。
拜托,请问,我们可以指出我应该采取哪些措施来获得我想要的东西?首先,将委托区分为不同的时间间隔(本周,下周,例如) - 从Parse中获取 - 然后将其分段排列?
我正在使用swift。
提前致谢!
答案 0 :(得分:2)
根据我的经验,PFQueryTableViewController为您节省了一些精力,并且需要付出很大的灵活性。使用常规UIViewController
子类的想法看起来像这样(将尝试使用最小代码,因为我意识到objective-c是OP的错误语言):
创建视图控制器,添加一个名为tableView的UITableView和插座。将表视图的数据源设置为视图控制器。
所有表视图都需要一个模型对象数组,而我们需要一个数组数组。这些将是按日期分组为三个数组的PFObject。所以我们需要一个函数来从解析中查询并对结果进行分组。
@property(weak) IBOutlet UITableView *tableView;
@property(strong) NSMutableArray *objects;
// in viewDidLoad, make an array of three arrays
self.objects = [NSMutableArray array];
[self.objects addObject:[NSMutableArray array]];
[self.objects addObject:[NSMutableArray array]];
[self.objects addObject:[NSMutableArray array]];
// in viewDidAppear, call the fetch
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self fetchData];
}
- (void)fetchData {
PFQuery *query = [PFQuery queryWithClassName:@"MyClass"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
[self insertModel:object];
}
[self.tableView reloadData];
}
}];
}
- (void)insertModel:(PFObject *)object {
NSDate *starts = [object valueForKey@"Starts"];
// dates are a complex topic in iOS, since you're working in swift
// its wasteful for me to go through the date calc in objective c.
// see this answer in SO for how to get dates like thisWeek, nextWeek, etc:
// http://stackoverflow.com/a/29056735/294949
NSMutableArray *soonModel = self.model[0];
NSMutableArray *thisWeekModel = self.model[1];
NSMutableArray *nextWeekModel = self.model[2];
if (/* starts in soon range */) [soonModel addObject:object];
if (/* starts in this week range */) [thisWeekModel addObject:object];
if (/* starts in next week range */) [nextWeekModel addObject:object];
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSArray *section = self.model[indexPath.section];
return section[indexPath.row];
}
这是第一个挑战。调用解析,并将结果分成三个数组。由于我们已经放弃了PFQueryTableVC,我们不需要跳过任何环节来使用它。我们只需像任何应用程序那样实现tableview数据源方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.model.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) return @"Soon";
if (section == 1) return @"This Week";
if (section == 2) return @"Next Week";
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *section = self.model[section];
return section.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// get your cell as you would in swift
PFObject *object = [self objectAtIndexPath:indexPath];
// configure cell with values from object
return cell;
}
答案 1 :(得分:2)
非常感谢@danh,为我指路!
我使用Swift工作了!我想在代码中有一些不好的做法,如果有人可以指出他们更好地帮助那些寻求问题的人,我会感激。
var container:NSMutableArray = NSMutableArray()
var thisWeek:NSMutableArray = NSMutableArray()
var nextWeek:NSMutableArray = NSMutableArray()
//FetchData
func fetchData() {
var query = PFQuery(className: "container")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if let staffObjects = objects as? [PFObject] {
for staff in staffObjects {
self.insertModel(staff)
}
self.tableView.reloadData()
}
} else {
NSLog("error: \(error)")
}
}
}
// Models
func insertModel(object: PFObject){
var starts: NSDate = (object.valueForKey("Starts") as? NSDate)!
// Here I managed to build the rules very easily using
// the SwiftDate Pod (https://github.com/malcommac/SwiftDate)
let today = NSDate.today()
let todayPlus7Days = today.add(years: 0, months: 0, weeks: 0, days: 7, hours: 0, minutes: 0, seconds: 0)
let todayPlus14Days = today.add(years: 0, months: 0, weeks: 0, days: 14, hours: 0, minutes: 0, seconds: 0)
if starts >= today && starts < todayPlus7Days {
thisWeek.addObject(object)
}
if starts >= todayPlus7Days && starts < todayPlus14Days{
nextWeek.addObject(object)
}
}
func objectAtIndexPath(indexPath: NSIndexPath) -> PFObject {
var section: NSArray = container[indexPath.section] as! NSArray
return section[indexPath.row] as! PFObject
}
// Table View
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return container.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 { return "NEXT 7 DAYS" }
if section == 1 { return "ANOTHERS 7 DAYS" }
return nil
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var section: NSArray = container[section] as! NSArray
return section.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:DestaqueTableViewCell? = tableView.dequeueReusableCellWithIdentifier("destaqueCell") as? DestaqueTableViewCell
let object: PFObject = objectAtIndexPath(indexPath)
// Configure cell with values from object
return cell!
}
// - viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
container.addObject(thisWeek)
container.addObject(nextWeek)
fetchData()
}