-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGJItemsViewController.m
206 lines (164 loc) · 7.14 KB
/
GJItemsViewController.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// GJItemsViewController.m
// Homepwner
//
// Created by 郭健 on 16/5/10.
// Copyright © 2016年 gj. All rights reserved.
//
#import "GJItemsViewController.h"
#import "GJItemStore.h"
#import "GJItem.h"
#import "GJDetailViewController.h"
#import "GJImageStore.h"
#import "GJImageViewController.h"
@interface GJItemsViewController () <UIPopoverControllerDelegate>
@property (nonatomic, strong) UIPopoverController *imagePopover;
//@property (nonatomic, strong) IBOutlet UIView *headerView;
@end
@implementation GJItemsViewController
//- (UIView *)headerView
//{
// if (!_headerView) {
// [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
// }
// return _headerView;
//}
- (IBAction)addNewItem:(id)sender
{
GJItem *newItem = [[GJItemStore sharedStore] createItem];
// NSInteger lastRow = [[[GJItemStore sharedStore] allItems] indexOfObject:newItem];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
//
// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
GJDetailViewController *detailViewController = [[GJDetailViewController alloc] initForNewItem:YES];
detailViewController.item = newItem;
detailViewController.dismissBlock = ^{
[self.tableView reloadData];
};
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
// navController.modalPresentationStyle = UIModalPresentationCurrentContext;
// self.definesPresentationContext = YES;
// navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:navController animated:YES completion:nil];
}
//- (IBAction)toggleEditingMode:(id)sender
//{
// if (self.isEditing) {
// [sender setTitle:@"Edit" forState:UIControlStateNormal];
//
// [self setEditing:NO animated:YES];
// } else {
// [sender setTitle:@"Done" forState:UIControlStateNormal];
//
// [self setEditing:YES animated:YES];
// }
//}
- (instancetype) init
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = @"Homepwner";
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem:)];
navItem.rightBarButtonItem = bbi;
navItem.leftBarButtonItem = self.editButtonItem;
// for (int i = 0; i < 5; i++) {
// [[GJItemStore sharedStore] createItem];
// }
}
return self;
}
- (instancetype) initWithStyle:(UITableViewStyle)style
{
return [self init];
}
//- (BOOL) prefersStatusBarHidden
//{
// return YES;
//}
- (void) viewDidLoad
{
[super viewDidLoad];
// UIView *header = self.headerView;
// [self.tableView setTableHeaderView:header];
// [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
UINib *nib = [UINib nibWithNibName:@"GJItemCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"GJItemCell"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// GJDetailViewController *detailViewController = [[GJDetailViewController alloc] init];
GJDetailViewController *detailViewController = [[GJDetailViewController alloc] initForNewItem:NO];
NSArray *items = [[GJItemStore sharedStore] allItems];
GJItem *selectedItem = items[indexPath.row];
detailViewController.item = selectedItem;
[self.navigationController pushViewController:detailViewController animated:YES];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
#pragma mark - dataSource方法
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[GJItemStore sharedStore] allItems] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
GJItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GJItemCell" forIndexPath:indexPath];
NSArray *items = [[GJItemStore sharedStore] allItems];
GJItem *item = items[indexPath.row];
// cell.textLabel.text = [item description];
cell.nameLabel.text = item.itemName;
cell.serialNumberLabel.text = item.serialNumber;
cell.valueLabel.text = [NSString stringWithFormat:@"$%d", item.valueInDollars];
if (item.valueInDollars > 50) {
cell.valueLabel.textColor = [UIColor greenColor];
}else{
cell.valueLabel.textColor = [UIColor redColor];
}
cell.thumbnailView.image = item.thumbnail;
// __weak *weakCell = cell;
cell.actionBlock = ^{
NSLog(@"Going to show image for %@", item);
// GJItemCell *strongCell = weakCell;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSString *itemKey = item.itemKey;
UIImage *img = [[GJImageStore sharedStore] imageForKey:itemKey];
if (!img) {
return;
}
CGRect rect = [self.view convertRect:cell.thumbnailView.bounds fromView:cell.thumbnailView];
// CGRect rect = [self.view convertRect:strongCell.thumbnailView.bounds fromView:strongCell.thumbnailView];
GJImageViewController *ivc = [[GJImageViewController alloc] init];
ivc.image = img;
self.imagePopover = [[UIPopoverController alloc] initWithContentViewController:ivc];
self.imagePopover.delegate = self;
self.imagePopover.popoverContentSize = CGSizeMake(600, 600);
[self.imagePopover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
};
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[GJItemStore sharedStore] allItems];
GJItem *item = items[indexPath.row];
[[GJItemStore sharedStore] removeItem:item];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[GJItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
}
#pragma mark - popover delegate method
-(void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
self.imagePopover = nil;
}
@end