ios - PFQueryTableViewController 快速按日期划分

标签 ios objective-c uitableview swift parse-platform

首先,这可能是一个菜鸟问题,但我尝试解决它有一段时间了,但我无法解决。

目标:

使用 [PFQueryTableViewController][1],我想要有与日期相关的部分。

示例:本周、下周、很快。

背景:

有关在 PFQueryTableViewController 上添加部分的问题在此 [解析主题][2] 中讨论。但该方法与日期间隔无关,我未能使其适应我的目标。

问题:

请问你们能指出我应该采取什么步骤才能得到我想要的东西吗?首先,区分不同时间间隔(例如本周、下周)的条目 - 从 Parse 获取 - 然后分段排列?

我正在使用 swift。

提前致谢!


编辑

@danh 建议放弃 PFQTVC 并启动一个新的 ViewController:这种方法的代码,满足问题所要求的内容,可以在 Objective C 的答案中找到 swift

最佳答案

根据我的经验,PFQueryTableViewController 可以为您节省一点精力,但会牺牲很大的灵活性。常规 UIViewController 子类的想法如下所示(将尝试使用最少的代码,因为我意识到 Objective-C 对于 OP 来说是错误的语言):

创建 View Controller ,添加一个 UITableView 和一个名为 tableView 的 socket 。将 TableView 的数据源设置为 View Controller 。

所有 TableView 都需要一个模型对象数组,而我们的 TableView 需要一个数组数组。这些将是按日期分为三个数组的 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];
}

这是第一个挑战。调用parse,并将结果分为三个数组。由于我们已经放弃了 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;
}

关于ios - PFQueryTableViewController 快速按日期划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30426804/

相关文章:

ios - 我想从数组中的数组中搜索?参见示例

ios - 没有GPS如何获取位置?

iphone - UIScrollView 未检测到折叠后的触摸

objective-c - 在 Obj-C 中使用 C 语法调用静态方法?

objective-c - 设备无连接时发出警报

ios - 如何在 xcode ios Swift 中为 TableView 后面的 View 着色?

ios - Swift:如何将不同的 UITableViewCell 执行 Segue 到不同的 ViewController?

ios - 在 Xcode 中使用一种图标大小

ios - CoreData - 建模映射表

ios - 委托(delegate)语法