├── README.md
├── CustomSearchField
├── Assets.xcassets
│ ├── Contents.json
│ ├── background.imageset
│ │ ├── background.png
│ │ └── Contents.json
│ └── AppIcon.appiconset
│ │ └── Contents.json
├── Model
│ ├── CustomSearchField.xcdatamodeld
│ │ ├── .xccurrentversion
│ │ └── CustomSearchField.xcdatamodel
│ │ │ └── contents
│ └── SearchItem.swift
├── Controllers
│ ├── ViewController.swift
│ └── CustomSearchTextField.swift
├── Info.plist
├── Base.lproj
│ └── LaunchScreen.storyboard
├── AppDelegate.swift
└── View
│ └── Base.lproj
│ └── Main.storyboard
├── CustomSearchField.xcodeproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
├── xcuserdata
│ └── sinitame.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
└── project.pbxproj
└── .gitignore
/README.md:
--------------------------------------------------------------------------------
1 | # Custom search text field
2 |
3 | This is a custom search field implemented with a tableView and coreData
4 |
--------------------------------------------------------------------------------
/CustomSearchField/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/CustomSearchField/Assets.xcassets/background.imageset/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sinitame/customSearchField-medium/HEAD/CustomSearchField/Assets.xcassets/background.imageset/background.png
--------------------------------------------------------------------------------
/CustomSearchField.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/CustomSearchField.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/CustomSearchField/Model/CustomSearchField.xcdatamodeld/.xccurrentversion:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | _XCCurrentVersionName
6 | CustomSearchField.xcdatamodel
7 |
8 |
9 |
--------------------------------------------------------------------------------
/CustomSearchField/Assets.xcassets/background.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "background.png",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "universal",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/CustomSearchField.xcodeproj/xcuserdata/sinitame.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | CustomSearchField.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/CustomSearchField/Controllers/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // CustomSearchField
4 | //
5 | // Created by Emrick Sinitambirivoutin on 19/02/2019.
6 | // Copyright © 2019 Emrick Sinitambirivoutin. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ViewController: UIViewController {
12 |
13 | override func viewDidLoad() {
14 | super.viewDidLoad()
15 | // Do any additional setup after loading the view, typically from a nib.
16 | }
17 |
18 | @IBAction func searchTextField(_ sender: CustomSearchTextField) {
19 | }
20 |
21 | }
22 |
23 |
--------------------------------------------------------------------------------
/CustomSearchField/Model/CustomSearchField.xcdatamodeld/CustomSearchField.xcdatamodel/contents:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CustomSearchField/Model/SearchItem.swift:
--------------------------------------------------------------------------------
1 | //
2 | // SearchItem.swift
3 | // CustomSearchField
4 | //
5 | // Created by Emrick Sinitambirivoutin on 20/02/2019.
6 | // Copyright © 2019 Emrick Sinitambirivoutin. All rights reserved.
7 | //
8 |
9 | import Foundation
10 |
11 | class SearchItem {
12 | var attributedCityName: NSMutableAttributedString?
13 | var attributedCountryName: NSMutableAttributedString?
14 | var allAttributedName : NSMutableAttributedString?
15 |
16 | var cityName: String
17 | var countryName: String
18 |
19 | public init(cityName: String, countryName: String) {
20 | self.cityName = cityName
21 | self.countryName = countryName
22 | }
23 |
24 | public func getFormatedText() -> NSMutableAttributedString{
25 | allAttributedName = NSMutableAttributedString()
26 | allAttributedName!.append(attributedCityName!)
27 | allAttributedName!.append(NSMutableAttributedString(string: ", "))
28 | allAttributedName!.append(attributedCountryName!)
29 |
30 | return allAttributedName!
31 | }
32 |
33 | public func getStringText() -> String{
34 | return "\(cityName), \(countryName)"
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/CustomSearchField/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/CustomSearchField/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ### Swift ###
2 | # Xcode
3 | #
4 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
5 |
6 | ## Build generated
7 | build/
8 | DerivedData/
9 |
10 | ## Various settings
11 | *.pbxuser
12 | !default.pbxuser
13 | *.mode1v3
14 | !default.mode1v3
15 | *.mode2v3
16 | !default.mode2v3
17 | *.perspectivev3
18 | !default.perspectivev3
19 | xcuserdata/
20 |
21 | ## Other
22 | *.moved-aside
23 | *.xccheckout
24 | *.xcscmblueprint
25 |
26 | ## Obj-C/Swift specific
27 | *.hmap
28 | *.ipa
29 | *.dSYM.zip
30 | *.dSYM
31 |
32 | ## Playgrounds
33 | timeline.xctimeline
34 | playground.xcworkspace
35 |
36 | # Swift Package Manager
37 | #
38 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
39 | # Packages/
40 | # Package.pins
41 | .build/
42 |
43 | # CocoaPods - Refactored to standalone file
44 |
45 | # Carthage - Refactored to standalone file
46 |
47 | # fastlane
48 | #
49 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
50 | # screenshots whenever they are needed.
51 | # For more information about the recommended setup visit:
52 | # https://docs.fastlane.tools/best-practices/source-control/#source-control
53 |
54 | fastlane/report.xml
55 | fastlane/Preview.html
56 | fastlane/screenshots
57 | fastlane/test_output
58 |
59 | ### Xcode ###
60 | # Xcode
61 | #
62 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
63 |
64 | ## Build generated
65 |
66 | ## Various settings
67 |
68 | ## Other
69 |
70 | ### Xcode Patch ###
71 | *.xcodeproj/*
72 | !*.xcodeproj/project.pbxproj
73 | !*.xcodeproj/xcshareddata/
74 | !*.xcworkspace/contents.xcworkspacedata
75 | /*.gcno
76 |
77 | # End of https://www.gitignore.io/api/swift,xcode
78 |
--------------------------------------------------------------------------------
/CustomSearchField/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/CustomSearchField/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // CustomSearchField
4 | //
5 | // Created by Emrick Sinitambirivoutin on 19/02/2019.
6 | // Copyright © 2019 Emrick Sinitambirivoutin. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | import CoreData
11 |
12 | @UIApplicationMain
13 | class AppDelegate: UIResponder, UIApplicationDelegate {
14 |
15 | var window: UIWindow?
16 |
17 |
18 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
19 | // Override point for customization after application launch.
20 | return true
21 | }
22 |
23 | func applicationWillResignActive(_ application: UIApplication) {
24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
25 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
26 | }
27 |
28 | func applicationDidEnterBackground(_ application: UIApplication) {
29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31 | }
32 |
33 | func applicationWillEnterForeground(_ application: UIApplication) {
34 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
35 | }
36 |
37 | func applicationDidBecomeActive(_ application: UIApplication) {
38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
39 | }
40 |
41 | func applicationWillTerminate(_ application: UIApplication) {
42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
43 | // Saves changes in the application's managed object context before the application terminates.
44 | self.saveContext()
45 | }
46 |
47 | // MARK: - Core Data stack
48 |
49 | lazy var persistentContainer: NSPersistentContainer = {
50 | /*
51 | The persistent container for the application. This implementation
52 | creates and returns a container, having loaded the store for the
53 | application to it. This property is optional since there are legitimate
54 | error conditions that could cause the creation of the store to fail.
55 | */
56 | let container = NSPersistentContainer(name: "CustomSearchField")
57 | container.loadPersistentStores(completionHandler: { (storeDescription, error) in
58 | if let error = error as NSError? {
59 | // Replace this implementation with code to handle the error appropriately.
60 | // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
61 |
62 | /*
63 | Typical reasons for an error here include:
64 | * The parent directory does not exist, cannot be created, or disallows writing.
65 | * The persistent store is not accessible, due to permissions or data protection when the device is locked.
66 | * The device is out of space.
67 | * The store could not be migrated to the current model version.
68 | Check the error message to determine what the actual problem was.
69 | */
70 | fatalError("Unresolved error \(error), \(error.userInfo)")
71 | }
72 | })
73 | return container
74 | }()
75 |
76 | // MARK: - Core Data Saving support
77 |
78 | func saveContext () {
79 | let context = persistentContainer.viewContext
80 | if context.hasChanges {
81 | do {
82 | try context.save()
83 | } catch {
84 | // Replace this implementation with code to handle the error appropriately.
85 | // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
86 | let nserror = error as NSError
87 | fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
88 | }
89 | }
90 | }
91 |
92 | }
93 |
94 |
--------------------------------------------------------------------------------
/CustomSearchField/View/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
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 |
41 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/CustomSearchField/Controllers/CustomSearchTextField.swift:
--------------------------------------------------------------------------------
1 | //
2 | // CustomSearchTextField.swift
3 | // CustomSearchField
4 | //
5 | // Created by Emrick Sinitambirivoutin on 19/02/2019.
6 | // Copyright © 2019 Emrick Sinitambirivoutin. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | import CoreData
11 |
12 | class CustomSearchTextField: UITextField{
13 |
14 | var dataList : [Cities] = [Cities]()
15 | var resultsList : [SearchItem] = [SearchItem]()
16 | var tableView: UITableView?
17 |
18 | let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
19 |
20 | // Connecting the new element to the parent view
21 | open override func willMove(toWindow newWindow: UIWindow?) {
22 | super.willMove(toWindow: newWindow)
23 | tableView?.removeFromSuperview()
24 |
25 | }
26 |
27 | override open func willMove(toSuperview newSuperview: UIView?) {
28 | super.willMove(toSuperview: newSuperview)
29 |
30 | self.addTarget(self, action: #selector(CustomSearchTextField.textFieldDidChange), for: .editingChanged)
31 | self.addTarget(self, action: #selector(CustomSearchTextField.textFieldDidBeginEditing), for: .editingDidBegin)
32 | self.addTarget(self, action: #selector(CustomSearchTextField.textFieldDidEndEditing), for: .editingDidEnd)
33 | self.addTarget(self, action: #selector(CustomSearchTextField.textFieldDidEndEditingOnExit), for: .editingDidEndOnExit)
34 | }
35 |
36 |
37 | override open func layoutSubviews() {
38 | super.layoutSubviews()
39 | buildSearchTableView()
40 |
41 | }
42 |
43 |
44 | //////////////////////////////////////////////////////////////////////////////
45 | // Text Field related methods
46 | //////////////////////////////////////////////////////////////////////////////
47 |
48 | @objc open func textFieldDidChange(){
49 | print("Text changed ...")
50 | filter()
51 | updateSearchTableView()
52 | tableView?.isHidden = false
53 | }
54 |
55 | @objc open func textFieldDidBeginEditing() {
56 | print("Begin Editing")
57 | }
58 |
59 | @objc open func textFieldDidEndEditing() {
60 | print("End editing")
61 |
62 | }
63 |
64 | @objc open func textFieldDidEndEditingOnExit() {
65 | print("End on Exit")
66 | }
67 |
68 | //////////////////////////////////////////////////////////////////////////////
69 | // Data Handling methods
70 | //////////////////////////////////////////////////////////////////////////////
71 |
72 |
73 | // MARK: CoreData manipulation methods
74 |
75 | // Don't need this function in this case
76 | func saveItems() {
77 | print("Saving items")
78 | do {
79 | try context.save()
80 | } catch {
81 | print("Error while saving items: \(error)")
82 | }
83 | }
84 |
85 | func loadItems(withRequest request : NSFetchRequest) {
86 | print("loading items")
87 | do {
88 | dataList = try context.fetch(request)
89 | } catch {
90 | print("Error while fetching data: \(error)")
91 | }
92 | }
93 |
94 |
95 | // MARK: Filtering methods
96 |
97 | fileprivate func filter() {
98 | let predicate = NSPredicate(format: "cityName CONTAINS[cd] %@", self.text!)
99 | let request : NSFetchRequest = Cities.fetchRequest()
100 | request.predicate = predicate
101 |
102 | loadItems(withRequest : request)
103 |
104 | resultsList = []
105 |
106 | for i in 0 ..< dataList.count {
107 |
108 | let item = SearchItem(cityName: dataList[i].cityName!,countryName: dataList[i].countryName!)
109 |
110 | let cityFilterRange = (item.cityName as NSString).range(of: text!, options: .caseInsensitive)
111 | let countryFilterRange = (item.countryName as NSString).range(of: text!, options: .caseInsensitive)
112 |
113 | if cityFilterRange.location != NSNotFound {
114 | item.attributedCityName = NSMutableAttributedString(string: item.cityName)
115 | item.attributedCountryName = NSMutableAttributedString(string: item.countryName)
116 |
117 | item.attributedCityName!.setAttributes([.font: UIFont.boldSystemFont(ofSize: 17)], range: cityFilterRange)
118 | if countryFilterRange.location != NSNotFound {
119 | item.attributedCountryName!.setAttributes([.font: UIFont.boldSystemFont(ofSize: 17)], range: countryFilterRange)
120 | }
121 |
122 | resultsList.append(item)
123 | }
124 |
125 | }
126 |
127 | tableView?.reloadData()
128 | }
129 |
130 |
131 | }
132 |
133 | extension CustomSearchTextField: UITableViewDelegate, UITableViewDataSource {
134 |
135 |
136 | //////////////////////////////////////////////////////////////////////////////
137 | // Table View related methods
138 | //////////////////////////////////////////////////////////////////////////////
139 |
140 |
141 | // MARK: TableView creation and updating
142 |
143 | // Create SearchTableview
144 | func buildSearchTableView() {
145 |
146 | if let tableView = tableView {
147 | tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CustomSearchTextFieldCell")
148 | tableView.delegate = self
149 | tableView.dataSource = self
150 | self.window?.addSubview(tableView)
151 |
152 | } else {
153 | //addData()
154 | print("tableView created")
155 | tableView = UITableView(frame: CGRect.zero)
156 | }
157 |
158 | updateSearchTableView()
159 | }
160 |
161 | // Updating SearchtableView
162 | func updateSearchTableView() {
163 |
164 | if let tableView = tableView {
165 | superview?.bringSubviewToFront(tableView)
166 | var tableHeight: CGFloat = 0
167 | tableHeight = tableView.contentSize.height
168 |
169 | // Set a bottom margin of 10p
170 | if tableHeight < tableView.contentSize.height {
171 | tableHeight -= 10
172 | }
173 |
174 | // Set tableView frame
175 | var tableViewFrame = CGRect(x: 0, y: 0, width: frame.size.width - 4, height: tableHeight)
176 | tableViewFrame.origin = self.convert(tableViewFrame.origin, to: nil)
177 | tableViewFrame.origin.x += 2
178 | tableViewFrame.origin.y += frame.size.height + 2
179 | UIView.animate(withDuration: 0.2, animations: { [weak self] in
180 | self?.tableView?.frame = tableViewFrame
181 | })
182 |
183 | //Setting tableView style
184 | tableView.layer.masksToBounds = true
185 | tableView.separatorInset = UIEdgeInsets.zero
186 | tableView.layer.cornerRadius = 5.0
187 | tableView.separatorColor = UIColor.lightGray
188 | tableView.backgroundColor = UIColor.white.withAlphaComponent(0.4)
189 |
190 | if self.isFirstResponder {
191 | superview?.bringSubviewToFront(self)
192 | }
193 |
194 | tableView.reloadData()
195 | }
196 | }
197 |
198 |
199 |
200 | // MARK: TableViewDataSource methods
201 | public func numberOfSections(in tableView: UITableView) -> Int {
202 | return 1
203 | }
204 |
205 | public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
206 | print(resultsList.count)
207 | return resultsList.count
208 | }
209 |
210 | // MARK: TableViewDelegate methods
211 |
212 | //Adding rows in the tableview with the data from dataList
213 |
214 | func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
215 | let cell = tableView.dequeueReusableCell(withIdentifier: "CustomSearchTextFieldCell", for: indexPath) as UITableViewCell
216 | cell.backgroundColor = UIColor.clear
217 | cell.textLabel?.attributedText = resultsList[indexPath.row].getFormatedText()
218 | return cell
219 | }
220 |
221 | public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
222 | print("selected row")
223 | self.text = resultsList[indexPath.row].getStringText()
224 | tableView.isHidden = true
225 | self.endEditing(true)
226 | }
227 |
228 |
229 | // MARK: Early testing methods
230 | func addData(){
231 | let a = Cities(context: context)
232 | a.cityName = "Paris"
233 | a.countryName = "France"
234 | let b = Cities(context: context)
235 | b.cityName = "Porto"
236 | b.countryName = "France"
237 | let c = Cities(context: context)
238 | c.cityName = "Pavard"
239 | c.countryName = "France"
240 | let d = Cities(context: context)
241 | d.cityName = "Parole"
242 | d.countryName = "France"
243 | let e = Cities(context: context)
244 | e.cityName = "Paria"
245 | e.countryName = "France"
246 |
247 | saveItems()
248 |
249 | dataList.append(a)
250 | dataList.append(b)
251 | dataList.append(c)
252 | dataList.append(d)
253 | dataList.append(e)
254 | }
255 |
256 | }
257 |
--------------------------------------------------------------------------------
/CustomSearchField.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 50;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 311CAABD221C57410077FA7E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 311CAABC221C57410077FA7E /* AppDelegate.swift */; };
11 | 311CAABF221C57410077FA7E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 311CAABE221C57410077FA7E /* ViewController.swift */; };
12 | 311CAAC2221C57410077FA7E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 311CAAC0221C57410077FA7E /* Main.storyboard */; };
13 | 311CAAC5221C57410077FA7E /* CustomSearchField.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 311CAAC3221C57410077FA7E /* CustomSearchField.xcdatamodeld */; };
14 | 311CAAC7221C57430077FA7E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 311CAAC6221C57430077FA7E /* Assets.xcassets */; };
15 | 311CAACA221C57430077FA7E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 311CAAC8221C57430077FA7E /* LaunchScreen.storyboard */; };
16 | 311CAAD5221C60FC0077FA7E /* CustomSearchTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 311CAAD4221C60FC0077FA7E /* CustomSearchTextField.swift */; };
17 | 311CAAD7221D58A90077FA7E /* SearchItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 311CAAD6221D58A90077FA7E /* SearchItem.swift */; };
18 | /* End PBXBuildFile section */
19 |
20 | /* Begin PBXFileReference section */
21 | 311CAAB9221C57410077FA7E /* CustomSearchField.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CustomSearchField.app; sourceTree = BUILT_PRODUCTS_DIR; };
22 | 311CAABC221C57410077FA7E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
23 | 311CAABE221C57410077FA7E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
24 | 311CAAC1221C57410077FA7E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
25 | 311CAAC4221C57410077FA7E /* CustomSearchField.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = CustomSearchField.xcdatamodel; sourceTree = ""; };
26 | 311CAAC6221C57430077FA7E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
27 | 311CAAC9221C57430077FA7E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
28 | 311CAACB221C57430077FA7E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
29 | 311CAAD4221C60FC0077FA7E /* CustomSearchTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSearchTextField.swift; sourceTree = ""; };
30 | 311CAAD6221D58A90077FA7E /* SearchItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchItem.swift; sourceTree = ""; };
31 | /* End PBXFileReference section */
32 |
33 | /* Begin PBXFrameworksBuildPhase section */
34 | 311CAAB6221C57410077FA7E /* Frameworks */ = {
35 | isa = PBXFrameworksBuildPhase;
36 | buildActionMask = 2147483647;
37 | files = (
38 | );
39 | runOnlyForDeploymentPostprocessing = 0;
40 | };
41 | /* End PBXFrameworksBuildPhase section */
42 |
43 | /* Begin PBXGroup section */
44 | 311CAAB0221C57410077FA7E = {
45 | isa = PBXGroup;
46 | children = (
47 | 311CAABB221C57410077FA7E /* CustomSearchField */,
48 | 311CAABA221C57410077FA7E /* Products */,
49 | );
50 | sourceTree = "";
51 | };
52 | 311CAABA221C57410077FA7E /* Products */ = {
53 | isa = PBXGroup;
54 | children = (
55 | 311CAAB9221C57410077FA7E /* CustomSearchField.app */,
56 | );
57 | name = Products;
58 | sourceTree = "";
59 | };
60 | 311CAABB221C57410077FA7E /* CustomSearchField */ = {
61 | isa = PBXGroup;
62 | children = (
63 | 311CAAD3221C608C0077FA7E /* Controllers */,
64 | 311CAAD2221C60840077FA7E /* View */,
65 | 311CAAD1221C60720077FA7E /* Model */,
66 | 311CAABC221C57410077FA7E /* AppDelegate.swift */,
67 | 311CAAC6221C57430077FA7E /* Assets.xcassets */,
68 | 311CAAC8221C57430077FA7E /* LaunchScreen.storyboard */,
69 | 311CAACB221C57430077FA7E /* Info.plist */,
70 | );
71 | path = CustomSearchField;
72 | sourceTree = "";
73 | };
74 | 311CAAD1221C60720077FA7E /* Model */ = {
75 | isa = PBXGroup;
76 | children = (
77 | 311CAAC3221C57410077FA7E /* CustomSearchField.xcdatamodeld */,
78 | 311CAAD6221D58A90077FA7E /* SearchItem.swift */,
79 | );
80 | path = Model;
81 | sourceTree = "";
82 | };
83 | 311CAAD2221C60840077FA7E /* View */ = {
84 | isa = PBXGroup;
85 | children = (
86 | 311CAAC0221C57410077FA7E /* Main.storyboard */,
87 | );
88 | path = View;
89 | sourceTree = "";
90 | };
91 | 311CAAD3221C608C0077FA7E /* Controllers */ = {
92 | isa = PBXGroup;
93 | children = (
94 | 311CAABE221C57410077FA7E /* ViewController.swift */,
95 | 311CAAD4221C60FC0077FA7E /* CustomSearchTextField.swift */,
96 | );
97 | path = Controllers;
98 | sourceTree = "";
99 | };
100 | /* End PBXGroup section */
101 |
102 | /* Begin PBXNativeTarget section */
103 | 311CAAB8221C57410077FA7E /* CustomSearchField */ = {
104 | isa = PBXNativeTarget;
105 | buildConfigurationList = 311CAACE221C57430077FA7E /* Build configuration list for PBXNativeTarget "CustomSearchField" */;
106 | buildPhases = (
107 | 311CAAB5221C57410077FA7E /* Sources */,
108 | 311CAAB6221C57410077FA7E /* Frameworks */,
109 | 311CAAB7221C57410077FA7E /* Resources */,
110 | );
111 | buildRules = (
112 | );
113 | dependencies = (
114 | );
115 | name = CustomSearchField;
116 | productName = CustomSearchField;
117 | productReference = 311CAAB9221C57410077FA7E /* CustomSearchField.app */;
118 | productType = "com.apple.product-type.application";
119 | };
120 | /* End PBXNativeTarget section */
121 |
122 | /* Begin PBXProject section */
123 | 311CAAB1221C57410077FA7E /* Project object */ = {
124 | isa = PBXProject;
125 | attributes = {
126 | LastSwiftUpdateCheck = 1010;
127 | LastUpgradeCheck = 1010;
128 | ORGANIZATIONNAME = "Emrick Sinitambirivoutin";
129 | TargetAttributes = {
130 | 311CAAB8221C57410077FA7E = {
131 | CreatedOnToolsVersion = 10.1;
132 | };
133 | };
134 | };
135 | buildConfigurationList = 311CAAB4221C57410077FA7E /* Build configuration list for PBXProject "CustomSearchField" */;
136 | compatibilityVersion = "Xcode 9.3";
137 | developmentRegion = en;
138 | hasScannedForEncodings = 0;
139 | knownRegions = (
140 | en,
141 | Base,
142 | );
143 | mainGroup = 311CAAB0221C57410077FA7E;
144 | productRefGroup = 311CAABA221C57410077FA7E /* Products */;
145 | projectDirPath = "";
146 | projectRoot = "";
147 | targets = (
148 | 311CAAB8221C57410077FA7E /* CustomSearchField */,
149 | );
150 | };
151 | /* End PBXProject section */
152 |
153 | /* Begin PBXResourcesBuildPhase section */
154 | 311CAAB7221C57410077FA7E /* Resources */ = {
155 | isa = PBXResourcesBuildPhase;
156 | buildActionMask = 2147483647;
157 | files = (
158 | 311CAACA221C57430077FA7E /* LaunchScreen.storyboard in Resources */,
159 | 311CAAC7221C57430077FA7E /* Assets.xcassets in Resources */,
160 | 311CAAC2221C57410077FA7E /* Main.storyboard in Resources */,
161 | );
162 | runOnlyForDeploymentPostprocessing = 0;
163 | };
164 | /* End PBXResourcesBuildPhase section */
165 |
166 | /* Begin PBXSourcesBuildPhase section */
167 | 311CAAB5221C57410077FA7E /* Sources */ = {
168 | isa = PBXSourcesBuildPhase;
169 | buildActionMask = 2147483647;
170 | files = (
171 | 311CAAD5221C60FC0077FA7E /* CustomSearchTextField.swift in Sources */,
172 | 311CAABF221C57410077FA7E /* ViewController.swift in Sources */,
173 | 311CAABD221C57410077FA7E /* AppDelegate.swift in Sources */,
174 | 311CAAC5221C57410077FA7E /* CustomSearchField.xcdatamodeld in Sources */,
175 | 311CAAD7221D58A90077FA7E /* SearchItem.swift in Sources */,
176 | );
177 | runOnlyForDeploymentPostprocessing = 0;
178 | };
179 | /* End PBXSourcesBuildPhase section */
180 |
181 | /* Begin PBXVariantGroup section */
182 | 311CAAC0221C57410077FA7E /* Main.storyboard */ = {
183 | isa = PBXVariantGroup;
184 | children = (
185 | 311CAAC1221C57410077FA7E /* Base */,
186 | );
187 | name = Main.storyboard;
188 | sourceTree = "";
189 | };
190 | 311CAAC8221C57430077FA7E /* LaunchScreen.storyboard */ = {
191 | isa = PBXVariantGroup;
192 | children = (
193 | 311CAAC9221C57430077FA7E /* Base */,
194 | );
195 | name = LaunchScreen.storyboard;
196 | sourceTree = "";
197 | };
198 | /* End PBXVariantGroup section */
199 |
200 | /* Begin XCBuildConfiguration section */
201 | 311CAACC221C57430077FA7E /* Debug */ = {
202 | isa = XCBuildConfiguration;
203 | buildSettings = {
204 | ALWAYS_SEARCH_USER_PATHS = NO;
205 | CLANG_ANALYZER_NONNULL = YES;
206 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
207 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
208 | CLANG_CXX_LIBRARY = "libc++";
209 | CLANG_ENABLE_MODULES = YES;
210 | CLANG_ENABLE_OBJC_ARC = YES;
211 | CLANG_ENABLE_OBJC_WEAK = YES;
212 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
213 | CLANG_WARN_BOOL_CONVERSION = YES;
214 | CLANG_WARN_COMMA = YES;
215 | CLANG_WARN_CONSTANT_CONVERSION = YES;
216 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
217 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
218 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
219 | CLANG_WARN_EMPTY_BODY = YES;
220 | CLANG_WARN_ENUM_CONVERSION = YES;
221 | CLANG_WARN_INFINITE_RECURSION = YES;
222 | CLANG_WARN_INT_CONVERSION = YES;
223 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
224 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
225 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
226 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
227 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
228 | CLANG_WARN_STRICT_PROTOTYPES = YES;
229 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
230 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
231 | CLANG_WARN_UNREACHABLE_CODE = YES;
232 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
233 | CODE_SIGN_IDENTITY = "iPhone Developer";
234 | COPY_PHASE_STRIP = NO;
235 | DEBUG_INFORMATION_FORMAT = dwarf;
236 | ENABLE_STRICT_OBJC_MSGSEND = YES;
237 | ENABLE_TESTABILITY = YES;
238 | GCC_C_LANGUAGE_STANDARD = gnu11;
239 | GCC_DYNAMIC_NO_PIC = NO;
240 | GCC_NO_COMMON_BLOCKS = YES;
241 | GCC_OPTIMIZATION_LEVEL = 0;
242 | GCC_PREPROCESSOR_DEFINITIONS = (
243 | "DEBUG=1",
244 | "$(inherited)",
245 | );
246 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
247 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
248 | GCC_WARN_UNDECLARED_SELECTOR = YES;
249 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
250 | GCC_WARN_UNUSED_FUNCTION = YES;
251 | GCC_WARN_UNUSED_VARIABLE = YES;
252 | IPHONEOS_DEPLOYMENT_TARGET = 12.1;
253 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
254 | MTL_FAST_MATH = YES;
255 | ONLY_ACTIVE_ARCH = YES;
256 | SDKROOT = iphoneos;
257 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
258 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
259 | };
260 | name = Debug;
261 | };
262 | 311CAACD221C57430077FA7E /* Release */ = {
263 | isa = XCBuildConfiguration;
264 | buildSettings = {
265 | ALWAYS_SEARCH_USER_PATHS = NO;
266 | CLANG_ANALYZER_NONNULL = YES;
267 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
268 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
269 | CLANG_CXX_LIBRARY = "libc++";
270 | CLANG_ENABLE_MODULES = YES;
271 | CLANG_ENABLE_OBJC_ARC = YES;
272 | CLANG_ENABLE_OBJC_WEAK = YES;
273 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
274 | CLANG_WARN_BOOL_CONVERSION = YES;
275 | CLANG_WARN_COMMA = YES;
276 | CLANG_WARN_CONSTANT_CONVERSION = YES;
277 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
278 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
279 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
280 | CLANG_WARN_EMPTY_BODY = YES;
281 | CLANG_WARN_ENUM_CONVERSION = YES;
282 | CLANG_WARN_INFINITE_RECURSION = YES;
283 | CLANG_WARN_INT_CONVERSION = YES;
284 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
285 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
286 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
287 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
288 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
289 | CLANG_WARN_STRICT_PROTOTYPES = YES;
290 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
291 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
292 | CLANG_WARN_UNREACHABLE_CODE = YES;
293 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
294 | CODE_SIGN_IDENTITY = "iPhone Developer";
295 | COPY_PHASE_STRIP = NO;
296 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
297 | ENABLE_NS_ASSERTIONS = NO;
298 | ENABLE_STRICT_OBJC_MSGSEND = YES;
299 | GCC_C_LANGUAGE_STANDARD = gnu11;
300 | GCC_NO_COMMON_BLOCKS = YES;
301 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
302 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
303 | GCC_WARN_UNDECLARED_SELECTOR = YES;
304 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
305 | GCC_WARN_UNUSED_FUNCTION = YES;
306 | GCC_WARN_UNUSED_VARIABLE = YES;
307 | IPHONEOS_DEPLOYMENT_TARGET = 12.1;
308 | MTL_ENABLE_DEBUG_INFO = NO;
309 | MTL_FAST_MATH = YES;
310 | SDKROOT = iphoneos;
311 | SWIFT_COMPILATION_MODE = wholemodule;
312 | SWIFT_OPTIMIZATION_LEVEL = "-O";
313 | VALIDATE_PRODUCT = YES;
314 | };
315 | name = Release;
316 | };
317 | 311CAACF221C57430077FA7E /* Debug */ = {
318 | isa = XCBuildConfiguration;
319 | buildSettings = {
320 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
321 | CODE_SIGN_STYLE = Automatic;
322 | DEVELOPMENT_TEAM = 8P677JA588;
323 | INFOPLIST_FILE = CustomSearchField/Info.plist;
324 | LD_RUNPATH_SEARCH_PATHS = (
325 | "$(inherited)",
326 | "@executable_path/Frameworks",
327 | );
328 | PRODUCT_BUNDLE_IDENTIFIER = com.emrick.CustomSearchField;
329 | PRODUCT_NAME = "$(TARGET_NAME)";
330 | SWIFT_VERSION = 4.2;
331 | TARGETED_DEVICE_FAMILY = "1,2";
332 | };
333 | name = Debug;
334 | };
335 | 311CAAD0221C57430077FA7E /* Release */ = {
336 | isa = XCBuildConfiguration;
337 | buildSettings = {
338 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
339 | CODE_SIGN_STYLE = Automatic;
340 | DEVELOPMENT_TEAM = 8P677JA588;
341 | INFOPLIST_FILE = CustomSearchField/Info.plist;
342 | LD_RUNPATH_SEARCH_PATHS = (
343 | "$(inherited)",
344 | "@executable_path/Frameworks",
345 | );
346 | PRODUCT_BUNDLE_IDENTIFIER = com.emrick.CustomSearchField;
347 | PRODUCT_NAME = "$(TARGET_NAME)";
348 | SWIFT_VERSION = 4.2;
349 | TARGETED_DEVICE_FAMILY = "1,2";
350 | };
351 | name = Release;
352 | };
353 | /* End XCBuildConfiguration section */
354 |
355 | /* Begin XCConfigurationList section */
356 | 311CAAB4221C57410077FA7E /* Build configuration list for PBXProject "CustomSearchField" */ = {
357 | isa = XCConfigurationList;
358 | buildConfigurations = (
359 | 311CAACC221C57430077FA7E /* Debug */,
360 | 311CAACD221C57430077FA7E /* Release */,
361 | );
362 | defaultConfigurationIsVisible = 0;
363 | defaultConfigurationName = Release;
364 | };
365 | 311CAACE221C57430077FA7E /* Build configuration list for PBXNativeTarget "CustomSearchField" */ = {
366 | isa = XCConfigurationList;
367 | buildConfigurations = (
368 | 311CAACF221C57430077FA7E /* Debug */,
369 | 311CAAD0221C57430077FA7E /* Release */,
370 | );
371 | defaultConfigurationIsVisible = 0;
372 | defaultConfigurationName = Release;
373 | };
374 | /* End XCConfigurationList section */
375 |
376 | /* Begin XCVersionGroup section */
377 | 311CAAC3221C57410077FA7E /* CustomSearchField.xcdatamodeld */ = {
378 | isa = XCVersionGroup;
379 | children = (
380 | 311CAAC4221C57410077FA7E /* CustomSearchField.xcdatamodel */,
381 | );
382 | currentVersion = 311CAAC4221C57410077FA7E /* CustomSearchField.xcdatamodel */;
383 | path = CustomSearchField.xcdatamodeld;
384 | sourceTree = "";
385 | versionGroupType = wrapper.xcdatamodel;
386 | };
387 | /* End XCVersionGroup section */
388 | };
389 | rootObject = 311CAAB1221C57410077FA7E /* Project object */;
390 | }
391 |
--------------------------------------------------------------------------------