Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

TableViewWithMultipleCellTypes.xcodeproj/xcuserdata/abhishekbedi.xcuserdatad/xcschemes/xcschememanagement.plist

TableViewWithMultipleCellTypes.xcodeproj/project.xcworkspace/xcuserdata/abhishekbedi.xcuserdatad/UserInterfaceState.xcuserstate
12 changes: 12 additions & 0 deletions TableViewWithMultipleCellTypes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
36C0EC911EB00C4A0039B330 /* ServerData.json in Resources */ = {isa = PBXBuildFile; fileRef = 36C0EC901EB00C4A0039B330 /* ServerData.json */; };
36C0EC931EB00C710039B330 /* ProfileModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C0EC921EB00C710039B330 /* ProfileModel.swift */; };
36C0EC951EB00E290039B330 /* ProfileViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C0EC941EB00E290039B330 /* ProfileViewModel.swift */; };
CFB17DC11EEB34CF00C437D2 /* Protocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFB17DC01EEB34CF00C437D2 /* Protocols.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -48,6 +49,7 @@
36C0EC901EB00C4A0039B330 /* ServerData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = ServerData.json; sourceTree = "<group>"; };
36C0EC921EB00C710039B330 /* ProfileModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProfileModel.swift; sourceTree = "<group>"; };
36C0EC941EB00E290039B330 /* ProfileViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProfileViewModel.swift; sourceTree = "<group>"; };
CFB17DC01EEB34CF00C437D2 /* Protocols.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Protocols.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -101,6 +103,7 @@
36C0EC7E1EB00BFC0039B330 /* AppDelegate.swift */,
36C0EC801EB00BFC0039B330 /* ViewController.swift */,
36C0EC901EB00C4A0039B330 /* ServerData.json */,
CFB17DC21EEB34D300C437D2 /* Protocols */,
36C0EC821EB00BFC0039B330 /* Main.storyboard */,
36C0EC851EB00BFC0039B330 /* Assets.xcassets */,
36C0EC871EB00BFC0039B330 /* LaunchScreen.storyboard */,
Expand All @@ -111,6 +114,14 @@
path = TableViewWithMultipleCellTypes;
sourceTree = "<group>";
};
CFB17DC21EEB34D300C437D2 /* Protocols */ = {
isa = PBXGroup;
children = (
CFB17DC01EEB34CF00C437D2 /* Protocols.swift */,
);
path = Protocols;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -195,6 +206,7 @@
367B20181ED20BC0000F2BFA /* AttributeCell.swift in Sources */,
36C0EC811EB00BFC0039B330 /* ViewController.swift in Sources */,
36C0EC951EB00E290039B330 /* ProfileViewModel.swift in Sources */,
CFB17DC11EEB34CF00C437D2 /* Protocols.swift in Sources */,
36C0EC7F1EB00BFC0039B330 /* AppDelegate.swift in Sources */,
367B200C1ED204B2000F2BFA /* NamePictureCell.swift in Sources */,
367B201C1ED211F3000F2BFA /* EmailCell.swift in Sources */,
Expand Down
14 changes: 3 additions & 11 deletions TableViewWithMultipleCellTypes/AboutCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@

import UIKit

class AboutCell: UITableViewCell {

class AboutCell: UITableViewCell, ReusableView {
@IBOutlet weak var aboutLabel: UILabel?

var item: ProfileViewModelItem? {
didSet {
guard let item = item as? ProfileViewModelAboutItem else {
guard let item = item as? ProfileViewModelAboutItem else {
return
}

aboutLabel?.text = item.about
}
}

static var nib:UINib {
return UINib(nibName: identifier, bundle: nil)
}

static var identifier: String {
return String(describing: self)
}
}
10 changes: 1 addition & 9 deletions TableViewWithMultipleCellTypes/AttributeCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

class AttributeCell: UITableViewCell {
class AttributeCell: UITableViewCell, ReusableView {

@IBOutlet weak var titleLabel: UILabel?
@IBOutlet weak var valueLabel: UILabel?
Expand All @@ -19,12 +19,4 @@ class AttributeCell: UITableViewCell {
valueLabel?.text = item?.value
}
}

static var nib:UINib {
return UINib(nibName: identifier, bundle: nil)
}

static var identifier: String {
return String(describing: self)
}
}
8 changes: 4 additions & 4 deletions TableViewWithMultipleCellTypes/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="SVb-CZ-t9J">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.17" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="SVb-CZ-t9J">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.14"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<!--Profile-->
<scene sceneID="OJn-A7-DuF">
<objects>
<viewController id="7Gt-lP-tBH" customClass="ViewController" customModule="TableViewWithMultipleCellTypes" customModuleProvider="target" sceneMemberID="viewController">
Expand All @@ -34,7 +34,7 @@
<constraint firstItem="vNg-BY-fVY" firstAttribute="top" secondItem="171-hL-Lv1" secondAttribute="top" id="rtE-eD-IHq"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="wbi-41-quD"/>
<navigationItem key="navigationItem" title="Profile" id="wbi-41-quD"/>
<connections>
<outlet property="tableView" destination="vNg-BY-fVY" id="5Sa-Mp-Uff"/>
</connections>
Expand Down
10 changes: 1 addition & 9 deletions TableViewWithMultipleCellTypes/EmailCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

class EmailCell: UITableViewCell {
class EmailCell: UITableViewCell, ReusableView {

@IBOutlet weak var emailLabel: UILabel?

Expand All @@ -21,12 +21,4 @@ class EmailCell: UITableViewCell {
emailLabel?.text = item.email
}
}

static var nib:UINib {
return UINib(nibName: identifier, bundle: nil)
}

static var identifier: String {
return String(describing: self)
}
}
16 changes: 1 addition & 15 deletions TableViewWithMultipleCellTypes/FriendCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

class FriendCell: UITableViewCell {
class FriendCell: UITableViewCell, ReusableView {

@IBOutlet weak var pictureImageView: UIImageView?
@IBOutlet weak var nameLabel: UILabel?
Expand All @@ -27,14 +27,6 @@ class FriendCell: UITableViewCell {
}
}

static var nib:UINib {
return UINib(nibName: identifier, bundle: nil)
}

static var identifier: String {
return String(describing: self)
}

override func awakeFromNib() {
super.awakeFromNib()

Expand All @@ -44,12 +36,6 @@ class FriendCell: UITableViewCell {
pictureImageView?.backgroundColor = UIColor.lightGray
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

override func prepareForReuse() {
super.prepareForReuse()

Expand Down
11 changes: 1 addition & 10 deletions TableViewWithMultipleCellTypes/NamePictureCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

class NamePictureCell: UITableViewCell {
class NamePictureCell: UITableViewCell, ReusableView {

@IBOutlet weak var nameLabel: UILabel?
@IBOutlet weak var pictureImageView: UIImageView?
Expand All @@ -23,15 +23,6 @@ class NamePictureCell: UITableViewCell {
pictureImageView?.image = UIImage(named: item.pictureUrl)
}
}

static var nib:UINib {
return UINib(nibName: identifier, bundle: nil)
}

static var identifier: String {
return String(describing: self)
}

override func awakeFromNib() {
super.awakeFromNib()

Expand Down
46 changes: 41 additions & 5 deletions TableViewWithMultipleCellTypes/ProfileModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import Foundation

typealias JSONDictionary = [String: Any]

public func dataFromFile(_ filename: String) -> Data? {
@objc class TestClass: NSObject { }

Expand All @@ -28,17 +30,17 @@ class Profile {

init?(data: Data) {
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any], let body = json["data"] as? [String: Any] {
if let json = try JSONSerialization.jsonObject(with: data) as? JSONDictionary, let body = json["data"] as? JSONDictionary {
self.fullName = body["fullName"] as? String
self.pictureUrl = body["pictureUrl"] as? String
self.about = body["about"] as? String
self.email = body["email"] as? String

if let friends = body["friends"] as? [[String: Any]] {
if let friends = body["friends"] as? [JSONDictionary] {
self.friends = friends.map { Friend(json: $0) }
}

if let profileAttributes = body["profileAttributes"] as? [[String: Any]] {
if let profileAttributes = body["profileAttributes"] as? [JSONDictionary] {
self.profileAttributes = profileAttributes.map { Attribute(json: $0) }
}
}
Expand All @@ -47,13 +49,47 @@ class Profile {
return nil
}
}

func setupItems(fromProfile profile: Profile) -> [ProfileViewModelItem] {
var items = [ProfileViewModelItem]()

if let name = profile.fullName, let pictureUrl = profile.pictureUrl {
let nameAndPictureItem = ProfileViewModelNamePictureItem(name: name, pictureUrl: pictureUrl)
items.append(nameAndPictureItem)
}

if let about = profile.about {
let aboutItem = ProfileViewModelAboutItem(about: about)
items.append(aboutItem)
}

if let email = profile.email {
let dobItem = ProfileViewModelEmailItem(email: email)
items.append(dobItem)
}

let attributes = profile.profileAttributes
if !attributes.isEmpty {
let attributesItem = ProfileViewModeAttributeItem(attributes: attributes)
items.append(attributesItem)
}

let friends = profile.friends
if !profile.friends.isEmpty {
let friendsItem = ProfileViewModeFriendsItem(friends: friends)
items.append(friendsItem)
}

return items
}

}

class Friend {
var name: String?
var pictureUrl: String?

init(json: [String: Any]) {
init(json: JSONDictionary) {
self.name = json["name"] as? String
self.pictureUrl = json["pictureUrl"] as? String
}
Expand All @@ -63,7 +99,7 @@ class Attribute {
var key: String?
var value: String?

init(json: [String: Any]) {
init(json: JSONDictionary) {
self.key = json["key"] as? String
self.value = json["value"] as? String
}
Expand Down
Loading