Skip to content

Commit 2a5bdb5

Browse files
committed
chang code for searchbar
1 parent cad8c75 commit 2a5bdb5

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

CameraSpec/Controller/ViewController.swift

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ class ViewController: UIViewController{
1414
@IBOutlet weak var aboutAuthor: UIBarButtonItem!
1515
@IBOutlet weak var searchBar: UISearchBar!
1616

17-
let data = [Camera].self
18-
19-
var filteredData: [Camera] = []
17+
var searchCamera = cameraMany
18+
var searching = false
2019

2120
override func viewDidLoad() {
2221
super.viewDidLoad()
@@ -37,10 +36,6 @@ class ViewController: UIViewController{
3736
// Menambahkan Delegate Ke Table View
3837
cameraTableView.delegate = self
3938

40-
41-
// ??
42-
searchBar.delegate = self
43-
4439
}
4540

4641

@@ -54,11 +49,21 @@ class ViewController: UIViewController{
5449

5550
}
5651

57-
extension ViewController: UITableViewDataSource, UISearchBarDelegate {
52+
extension ViewController: UITableViewDataSource {
5853
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
5954

60-
// Menambahkan Total Item Yang Akan Muncul Di Dalam Table View
61-
return cameraMany.count
55+
if searching {
56+
57+
return searchCamera.count
58+
59+
} else {
60+
61+
// Menambahkan Total Item Yang Akan Muncul Di Dalam Table View
62+
return cameraMany.count
63+
64+
}
65+
66+
6267

6368
}
6469

@@ -70,29 +75,27 @@ extension ViewController: UITableViewDataSource, UISearchBarDelegate {
7075
// Menetapkan Nilai Hero Ke View Di Dalam Cell
7176
let camera = cameraMany[indexPath.row]
7277

73-
// Memasukkan Data Ke Setiap View Dan Label Yang Ada
74-
cell?.photoCamera.image = camera.photo
75-
cell?.nameCamera.text = camera.name
76-
cell?.descCamera.text = camera.description
78+
// ??
79+
if searching {
80+
cell?.nameCamera.text = searchCamera[indexPath.row]
81+
} else {
82+
83+
// Memasukkan Data Ke Setiap View Dan Label Yang Ada
84+
cell?.photoCamera.image = camera.photo
85+
cell?.nameCamera.text = camera.name
86+
cell?.descCamera.text = camera.description
87+
}
88+
89+
90+
7791

7892
// Membuat ImageView Mempunyai Radius
7993
cell?.photoCamera.layer.cornerRadius = 4
8094
cell?.photoCamera.clipsToBounds = true
8195

8296
return cell!
8397

84-
8598
}
86-
87-
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
88-
89-
filteredData = []
90-
91-
for in cameraMany {
92-
93-
}
94-
}
95-
9699
}
97100

98101

@@ -112,5 +115,12 @@ extension ViewController: UITableViewDelegate {
112115
tableView.deselectRow(at: indexPath , animated: true)
113116

114117
}
118+
}
115119

120+
extension ViewController: UISearchBarDelegate {
121+
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
122+
123+
124+
125+
}
116126
}

CameraSpec/View/Base.lproj/Main.storyboard

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15705" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="iUM-Pc-T3w">
3-
<device id="retina4_7" orientation="portrait" appearance="light"/>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="iUM-Pc-T3w">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
46
<dependencies>
5-
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15706"/>
7+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
78
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
89
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
910
</dependencies>
@@ -17,19 +18,22 @@
1718
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1819
<subviews>
1920
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="jvi-MF-vpJ">
20-
<rect key="frame" x="0.0" y="44" width="375" height="623"/>
21+
<rect key="frame" x="0.0" y="172" width="375" height="495"/>
2122
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
22-
<searchBar key="tableHeaderView" contentMode="redraw" id="i8j-HJ-7gc">
23-
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
24-
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
25-
<textInputTraits key="textInputTraits"/>
26-
</searchBar>
2723
</tableView>
24+
<searchBar contentMode="redraw" translatesAutoresizingMaskIntoConstraints="NO" id="i8j-HJ-7gc">
25+
<rect key="frame" x="0.0" y="116" width="375" height="56"/>
26+
<color key="barTintColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
27+
<textInputTraits key="textInputTraits"/>
28+
</searchBar>
2829
</subviews>
2930
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3031
<constraints>
31-
<constraint firstItem="jvi-MF-vpJ" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="8ee-Ab-fuO"/>
32+
<constraint firstItem="i8j-HJ-7gc" firstAttribute="trailing" secondItem="6Tk-OE-BBY" secondAttribute="trailing" id="BMI-Mw-Sdl"/>
33+
<constraint firstItem="jvi-MF-vpJ" firstAttribute="top" secondItem="i8j-HJ-7gc" secondAttribute="bottom" id="LCr-Gt-Qc9"/>
34+
<constraint firstItem="i8j-HJ-7gc" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="N28-Fx-BIR"/>
3235
<constraint firstItem="jvi-MF-vpJ" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="RpZ-XZ-fnT"/>
36+
<constraint firstItem="i8j-HJ-7gc" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="aBT-iQ-CrI"/>
3337
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="jvi-MF-vpJ" secondAttribute="trailing" id="dLw-WV-4GK"/>
3438
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="jvi-MF-vpJ" secondAttribute="bottom" id="uIl-T1-3Cl"/>
3539
</constraints>
@@ -58,7 +62,7 @@
5862
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="iUM-Pc-T3w" sceneMemberID="viewController">
5963
<toolbarItems/>
6064
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" largeTitles="YES" id="b8E-da-SRy">
61-
<rect key="frame" x="0.0" y="0.0" width="375" height="96"/>
65+
<rect key="frame" x="0.0" y="20" width="375" height="96"/>
6266
<autoresizingMask key="autoresizingMask"/>
6367
</navigationBar>
6468
<nil name="viewControllers"/>

0 commit comments

Comments
 (0)