-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleSelectionViewController.m
162 lines (99 loc) · 3.81 KB
/
SingleSelectionViewController.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
//
// SingleSelectionViewController.m
// Demo
//
// Created by Tallha Sarwar on 25/09/2016.
// Copyright (c) 2014 Pro. All rights reserved.
//
#import "SingleSelectionViewController.h"
@interface SingleSelectionViewController ()
@property(nonatomic,strong) NSArray * searchList;
@property (weak) IBOutlet NSTextField *noDataFoundLabel;
@property (weak) IBOutlet NSScrollView *scrollViewOutlet;
@end
@implementation SingleSelectionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void) loadView
{
[super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidChange:) name:NSControlTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidEndEditing:) name:NSControlTextDidEndEditingNotification object:nil];
self.tableView.selectionHighlightStyle=NSTableViewSelectionHighlightStyleNone;
self.tableView.headerView=nil;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;{
NSString *title = [self.delegate getTitleForCell:row fromList:[self localData]];
return title;
}
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row;{
id object = [[self localData] objectAtIndex:row];
[self.delegate objectSelected:object];
return YES;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;{
NSInteger count = [self localData].count;
if (count>0) {
self.noDataFoundLabel.hidden=YES;
_scrollViewOutlet.hidden=NO;
}
else
{
self.noDataFoundLabel.hidden=NO;
_scrollViewOutlet.hidden=YES;
}
return count;
return [self localData].count;
}
-(NSArray*) localData{
if(self.searching)
return self.searchList;
return self.dataList;
}
- (void)controlTextDidChange:(NSNotification *)obj {
// You can get the NSTextField, which is calling the method, through the userInfo dictionary.
NSSearchField *textField = obj.object;
if(textField == self.searchField)
{
if (!textField.stringValue.length) {
//end search
self.searching=NO;
[self.tableView reloadData];
}
else
{
//In search
self.searching=YES;
//apply predicate here and store it to searchList
// if (self.searchField.stringValue.length>2)
{
self.searchList=[self.dataList filteredArrayUsingPredicate:[self.delegate getSearchPredicateForText:textField.stringValue]];
[self.tableView reloadData];
}
}
}
}
- (void)controlTextDidEndEditing:(NSNotification *)obj{
// You can get the NSTextField, which is calling the method, through the userInfo dictionary.
NSSearchField *textField = obj.object;
if(textField == self.searchField)
{
if (!textField.stringValue.length) {
//end search
self.searching=NO;
[self.tableView reloadData];
}
}
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSControlTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSControlTextDidEndEditingNotification object:nil];
}
@end