Skip to content

Commit fe551ce

Browse files
author
cyper
committed
Discover TableViewController and CloudKit convenience API
1 parent b3d02d8 commit fe551ce

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

‎FoodPinDemo.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2E6F34881DC79C08002EE20D /* FoodPinDemo.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 2E6F34861DC79C08002EE20D /* FoodPinDemo.xcdatamodeld */; };
2121
2E6F348A1DC7A6B5002EE20D /* CD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E6F34891DC7A6B5002EE20D /* CD.swift */; };
2222
2E94A4EF1DCB2C4F00002DBB /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2E94A4EE1DCB2C4F00002DBB /* CloudKit.framework */; };
23+
2E94A4F11DCB317D00002DBB /* DiscoverTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E94A4F01DCB317D00002DBB /* DiscoverTableViewController.swift */; };
2324
2EA030B01DC9C2F100F67610 /* AboutTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EA030AF1DC9C2F100F67610 /* AboutTableViewController.swift */; };
2425
2EA030B21DC9CCDB00F67610 /* WebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EA030B11DC9CCDB00F67610 /* WebViewController.swift */; };
2526
2EB86CC71DC3AD42001A323A /* RestaurantDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EB86CC61DC3AD42001A323A /* RestaurantDetailViewController.swift */; };
@@ -49,6 +50,7 @@
4950
2E6F34891DC7A6B5002EE20D /* CD.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CD.swift; sourceTree = "<group>"; };
5051
2E94A4C21DCB284500002DBB /* FoodPinDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = FoodPinDemo.entitlements; sourceTree = "<group>"; };
5152
2E94A4EE1DCB2C4F00002DBB /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; };
53+
2E94A4F01DCB317D00002DBB /* DiscoverTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiscoverTableViewController.swift; sourceTree = "<group>"; };
5254
2EA030AF1DC9C2F100F67610 /* AboutTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AboutTableViewController.swift; sourceTree = "<group>"; };
5355
2EA030B11DC9CCDB00F67610 /* WebViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewController.swift; sourceTree = "<group>"; };
5456
2EB86CC61DC3AD42001A323A /* RestaurantDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestaurantDetailViewController.swift; sourceTree = "<group>"; };
@@ -121,6 +123,7 @@
121123
2E2F74A21DC65E7E00B1729B /* AddRestaurantController.swift */,
122124
2EA030AF1DC9C2F100F67610 /* AboutTableViewController.swift */,
123125
2EA030B11DC9CCDB00F67610 /* WebViewController.swift */,
126+
2E94A4F01DCB317D00002DBB /* DiscoverTableViewController.swift */,
124127
2EFCB2911DC8C81F0005CA3C /* PageContainer.swift */,
125128
2EFCB28F1DC8C2D60005CA3C /* SinglePage.swift */,
126129
2EB86CC81DC3BC9A001A323A /* README.md */,
@@ -229,6 +232,7 @@
229232
2E2F749C1DC5CF9C00B1729B /* ReviewViewController.swift in Sources */,
230233
2EA030B01DC9C2F100F67610 /* AboutTableViewController.swift in Sources */,
231234
2EFCB2901DC8C2D60005CA3C /* SinglePage.swift in Sources */,
235+
2E94A4F11DCB317D00002DBB /* DiscoverTableViewController.swift in Sources */,
232236
2E6F348A1DC7A6B5002EE20D /* CD.swift in Sources */,
233237
2EB86CCD1DC3D02B001A323A /* RestaurantDetailTableViewCell.swift in Sources */,
234238
2EA030B21DC9CCDB00F67610 /* WebViewController.swift in Sources */,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// DiscoverTableViewController.swift
3+
// FoodPinDemo
4+
//
5+
// Created by cyper on 03/11/2016.
6+
// Copyright © 2016 cyper tech. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import CloudKit
11+
12+
class DiscoverTableViewController: UITableViewController {
13+
var restaurants: [CKRecord] = []
14+
15+
override func viewDidLoad() {
16+
super.viewDidLoad()
17+
18+
fetchRecordsFromCloud()
19+
}
20+
21+
func fetchRecordsFromCloud() {
22+
// Fetch data using Convenience API
23+
let cloudContainer = CKContainer.default()
24+
let publicDb = cloudContainer.publicCloudDatabase
25+
let predicate = NSPredicate(value: true)
26+
let query = CKQuery(recordType: "Restaurant", predicate: predicate)
27+
28+
publicDb.perform(query, inZoneWith: nil, completionHandler: {
29+
(results, error) -> Void in
30+
if error != nil {
31+
print(error!)
32+
return
33+
}
34+
35+
if let results = results {
36+
print("completed the download of res data")
37+
self.restaurants = results
38+
OperationQueue.main.addOperation {
39+
self.tableView.reloadData()
40+
}
41+
}
42+
})
43+
}
44+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
45+
return restaurants.count
46+
}
47+
48+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
49+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
50+
let r = restaurants[indexPath.row]
51+
cell.textLabel?.text = r.object(forKey: "name") as? String
52+
53+
if let image = r.object(forKey: "image") {
54+
let imageAsset = image as! CKAsset
55+
if let imageData = try? Data.init(contentsOf: imageAsset.fileURL) {
56+
cell.imageView?.image = UIImage(data: imageData)
57+
}
58+
}
59+
60+
return cell
61+
}
62+
}

‎FoodPinDemo/discover.storyboard‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Xvw-ex-AzY">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
7+
<deployment identifier="iOS"/>
48
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
59
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
610
</dependencies>
711
<scenes>
812
<!--Discover-->
913
<scene sceneID="SoY-jv-PJx">
1014
<objects>
11-
<tableViewController id="dh2-RF-Qbx" sceneMemberID="viewController">
15+
<tableViewController id="dh2-RF-Qbx" customClass="DiscoverTableViewController" customModule="FoodPinDemo" customModuleProvider="target" sceneMemberID="viewController">
1216
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="Wmq-KW-2YU">
1317
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1418
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1519
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
1620
<prototypes>
17-
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="bby-JK-ZTQ">
21+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" textLabel="EUu-5h-O0r" style="IBUITableViewCellStyleDefault" id="bby-JK-ZTQ">
1822
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
1923
<autoresizingMask key="autoresizingMask"/>
2024
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="bby-JK-ZTQ" id="4iu-mK-RFO">
21-
<rect key="frame" x="0.0" y="0.0" width="375" height="43"/>
25+
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
2226
<autoresizingMask key="autoresizingMask"/>
27+
<subviews>
28+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="EUu-5h-O0r">
29+
<rect key="frame" x="15" y="0.0" width="345" height="43.5"/>
30+
<autoresizingMask key="autoresizingMask"/>
31+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
32+
<nil key="textColor"/>
33+
<nil key="highlightedColor"/>
34+
</label>
35+
</subviews>
2336
</tableViewCellContentView>
2437
</tableViewCell>
2538
</prototypes>
@@ -40,7 +53,6 @@
4053
<navigationController id="Xvw-ex-AzY" sceneMemberID="viewController">
4154
<tabBarItem key="tabBarItem" title="Discover" image="discover" id="8iS-1f-M13"/>
4255
<simulatedTabBarMetrics key="simulatedBottomBarMetrics"/>
43-
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4_7.fullscreen"/>
4456
<navigationBar key="navigationBar" contentMode="scaleToFill" id="Y5w-Hg-QWj">
4557
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
4658
<autoresizingMask key="autoresizingMask"/>

0 commit comments

Comments
 (0)