forked from marcoarment/FCModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
81 lines (62 loc) · 2.23 KB
/
ViewController.m
File metadata and controls
81 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// ViewController.m
// FCModelTest
//
// Created by Marco Arment on 9/14/13.
// Copyright (c) 2013 Marco Arment. All rights reserved.
//
#import "ViewController.h"
#import "PersonCell.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic, copy) NSArray *people;
@end
@implementation ViewController
- (id)init { return [super initWithNibName:@"ViewController" bundle:nil]; }
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *cellNib = [UINib nibWithNibName:@"PersonCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"PersonCell"];
[self reloadPeople:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(reloadPeople:) name:FCModelChangeNotification object:Person.class];
}
- (void)reloadPeople:(NSNotification *)notification
{
self.people = [Person allInstances];
NSLog(@"Reloading with %lu people", (unsigned long) self.people.count);
[self.collectionView reloadData];
}
- (void)dealloc
{
[NSNotificationCenter.defaultCenter removeObserver:self name:FCModelChangeNotification object:Person.class];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[Person executeUpdateQuery:self.queryField.text arguments:nil];
return NO;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; }
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.people.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PersonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonCell" forIndexPath:indexPath];
[cell configureWithPerson:self.people[indexPath.row]];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
Person *p = (Person *) self.people[indexPath.row];
[p save:^{
p.taps++;
}];
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
}
@end