-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCQFormViewController.m
115 lines (92 loc) · 3.17 KB
/
CQFormViewController.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
/*
Copyright (C) 2012 Quentin Mathe
Author: Quentin Mathe <[email protected]>
Date: June 2012
License: MIT
*/
#import "CQFormViewController.h"
#import "CQFormItem.h"
#import "CQFormView.h"
#import "CQMacros.h"
@implementation CQFormViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//assert(self.formView.superview == self.view || self.formView == self.view);
assert(self.formView.dataSource == self.formView);
}
- (void)setFormView: (CQFormView *)aFormView
{
_formView = aFormView;
if (_formView.delegate == nil)
{
_formView.delegate = self;
}
}
- (BOOL)presentContentForItem: (CQFormItem *)item
{
if (self.navigationController == nil || item.contentViewController == nil)
return NO;
if (item.contentViewController.title == nil)
{
item.contentViewController.title = item.label;
}
[self.navigationController pushViewController: item.contentViewController
animated: YES];
return YES;
}
#pragma mark - Table View Delegate
// NOTE: UITableView asks the delegate for the row height before calling
// -tableView:cellForRowAtIndexPath: (so calling the later in this delegate
// method causes a crash).
- (CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
// TODO: If UITableView.rowHeight returns UITableViewAutomaticDimension,
// then returns this rather than the item height as we do. In this way,
// we would support both iOS 7 and 8 (including the new automatic cell sizing).
return [self.formView itemForIndexPath: indexPath].view.frame.size.height;
}
// NOTE: For normal form rows, [CQFormItem isHighlightable] returns NO and
// prevents selection-related delegate methods to be called.
// Highlight happens on touch down while select happens on touch up.
- (BOOL)tableView: (UITableView *)tableView shouldHighlightRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSLog(@"Should highlight %@", indexPath);
return [[self.formView itemForIndexPath: indexPath] isHighlightable];
}
- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSLog(@"Will select %@", indexPath);
return indexPath;
}
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSLog(@"Did select %@", indexPath);
CQFormItem *item = [self.formView itemForIndexPath: indexPath];
// Will call the select block or update the affected value when the item
// represents an option
[item didSelect];
BOOL presentedContent = [self presentContentForItem: item];
if (presentedContent)
{
[tableView deselectRowAtIndexPath: indexPath animated: NO];
}
else if (item.selectBlock == NULL)
{
// Will add a checkmark and deselect
[self.formView checkRowAtIndexPath: indexPath animated: YES];
}
}
- (void)tableView: (UITableView *)tableView didDeselectRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSLog(@"Did deselect %@", indexPath);
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"Did highlight %@", indexPath);
}
- (void)tableView: (UITableView *)tableView didUnhighlightRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSLog(@"Did unhighlight %@", indexPath);
}
@end