Done simple table view example with twitter search api
Seto Elkahfi committed
Apr 15, 2017 at 17:38 UTC
d222e902a76f5c409a3cf3f38793488b37f461bb
4 files changed
+76
-9
Example/JomloTableView/Base.lproj/Main.storyboard
+4
-1
@@ -39,13 +39,16 @@
39
<!--SimpleTableView-->
40
<scene sceneID="ZkZ-li-643">
41
<objects>
42
- <viewController id="2ow-KJ-V6C" sceneMemberID="viewController">
42
+ <viewController id="2ow-KJ-V6C" customClass="SimpleTableViewController" customModule="JomloTableView_Example" customModuleProvider="target" sceneMemberID="viewController">
43
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="ewI-eZ-BEt" customClass="JomloTableView" customModule="JomloTableView">
44
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
45
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
46
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
47
</tableView>
48
<navigationItem key="navigationItem" title="SimpleTableView" id="wl8-gL-Vnq"/>
49
+ <connections>
50
+ <outlet property="jomloTableView" destination="ewI-eZ-BEt" id="OO1-dw-ETv"/>
51
+ </connections>
52
</viewController>
53
<placeholder placeholderIdentifier="IBFirstResponder" id="G8B-Nb-fl6" userLabel="First Responder" sceneMemberID="firstResponder"/>
54
</objects>
Example/JomloTableView/Rows/SimpleCell.swift
-1
@@ -26,7 +26,6 @@ class SimpleRow: JomloTableViewRow {
26
self.subTitle = subTitle
27
}
28
29
-
29
override var identifier: String {
30
return "SimpleCell"
31
}
Example/JomloTableView/Rows/SimpleCell.xib
+2
-2
@@ -15,7 +15,7 @@
15
<rect key="frame" x="0.0" y="0.0" width="598" height="120"/>
16
<autoresizingMask key="autoresizingMask"/>
17
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Jxp-d1-zT1" id="150-Wq-GWr">
18
- <rect key="frame" x="0.0" y="0.0" width="598" height="120"/>
18
+ <rect key="frame" x="0.0" y="0.0" width="598" height="119.5"/>
19
<autoresizingMask key="autoresizingMask"/>
20
<subviews>
21
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fUm-db-ewC" userLabel="Title">
@@ -27,7 +27,7 @@
27
<label opaque="NO" userInteractionEnabled="NO" contentMode="top" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Multi line explanation" textAlignment="justified" lineBreakMode="tailTruncation" numberOfLines="4" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="c4C-y0-zNM" userLabel="Sub title">
28
<rect key="frame" x="8" y="28" width="582" height="84"/>
29
<fontDescription key="fontDescription" type="italicSystem" pointSize="14"/>
30
- <color key="textColor" red="0.50196081400000003" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
30
+ <color key="textColor" red="0.0" green="0.48005890846252441" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
31
<nil key="highlightedColor"/>
32
</label>
33
</subviews>
Example/JomloTableView/View Controllers/SimpleTableViewController.swift
+70
-5
@@ -7,18 +7,83 @@
7
//
8
9
import UIKit
10
+import JomloTableView
11
+import TwitterKit
12
13
class SimpleTableViewController: UIViewController {
14
15
+ @IBOutlet var jomloTableView: JomloTableView!
16
+
17
+ let tableSection = JomloTableViewSection()
18
+ let client = TWTRAPIClient()
19
+ var loginButton: TWTRLogInButton!
20
+
21
override func viewDidLoad() {
22
super.viewDidLoad()
15
-
16
- // Do any additional setup after loading the view.
23
+
24
+ jomloTableView.addSection(section: tableSection)
25
+
26
+ loginButton = TWTRLogInButton { (session, error) in
27
+ if let session = session {
28
+ print("Signed as: \(session.userName)")
29
+ self.loadTweets()
30
+ } else {
31
+ print("Error: \(error?.localizedDescription)")
32
+ }
33
+ }
34
+ loginButton.center = view.center
35
+ view.addSubview(loginButton)
36
+
37
}
38
19
- override func didReceiveMemoryWarning() {
20
- super.didReceiveMemoryWarning()
21
- // Dispose of any resources that can be recreated.
39
+ internal func loadTweets() {
40
+
41
+ let searchEndPoint = "https://api.twitter.com/1.1/search/tweets.json"
42
+ let params = ["q" : "jomlo"]
43
+ var clientError: NSError?
44
+
45
+ let request = client.urlRequest(withMethod: "GET", url: searchEndPoint, parameters: params, error: &clientError)
46
+
47
+ client.sendTwitterRequest(request) { (response, data, connectionError) in
48
+
49
+ if let data = data {
50
+ self.populate(data: data)
51
+ }
52
+
53
+ if let error = connectionError {
54
+ print("Error: \(error.localizedDescription)")
55
+ }
56
+
57
+
58
+ }
59
}
60
61
+ internal func populate(data: Data) {
62
+ do {
63
+ if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
64
+// print("json: \(json)")
65
+ if let statuses = json["statuses"] as? [Any] {
66
+// print("statuses: \(statuses)")
67
+ for status in statuses {
68
+ print("status: \(status)")
69
+ let status = status as! [String: Any]
70
+// print("\(status["text"])")
71
+ let user = status["user"] as! [String: Any]
72
+
73
+ let row = SimpleRow("@\(user["screen_name"] as! String)", subTitle: status["text"] as! String)
74
+ tableSection.addRow(row: row)
75
+
76
+ }
77
+ DispatchQueue.main.async {
78
+ self.loginButton.removeFromSuperview()
79
+ self.jomloTableView.reloadData()
80
+ }
81
+ }
82
+ }
83
+
84
+
85
+ } catch let jsonError as NSError {
86
+ print("json error: \(jsonError.localizedDescription)")
87
+ }
88
+ }
89
}