forked from colatusso/iOSAppTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListViewController.m
109 lines (87 loc) · 2.93 KB
/
ListViewController.m
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// ListViewController.m
// Template 1
//
// Created by Rafael on 05/12/13.
// Copyright (c) 2013 Rafael Colatusso. All rights reserved.
//
#import "ListViewController.h"
#import "MenuTableViewController.h"
@interface ListViewController ()
@end
@implementation ListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// listen for new rows added via AddToDoViewController
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"ToDoDataUpdated"
object:nil];
[self reloadDataFromParse];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self reloadDataFromParse];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.toDoArray objectAtIndex:indexPath.row] objectForKey:@"text"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// delete the row from parse and fade out the cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFObject *object = [self.toDoArray objectAtIndex:indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[UIView animateWithDuration:1.0f animations:^{
cell.alpha = 0;
} completion:^(BOOL finished) {
[self reloadDataFromParse];
}];
}];
cell.selected = NO;
}
#pragma mark -
#pragma mark Helper methods
- (void)reloadDataFromParse {
// retrieve all objects from ToDoList class at Parse
self.toDoArray = [NSArray array];
self.query = [PFQuery queryWithClassName:@"ToDoList"];
[self.query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error) {
self.toDoArray = objects;
[self.tableView reloadData];
}
}];
}
-(void)handleUpdatedData:(NSNotification *)notification {
// when notificated reload the data
[self reloadDataFromParse];
}
@end