Skip to content

Commit 0d7480e

Browse files
Tyler FoxTyler Fox
Tyler Fox
authored and
Tyler Fox
committed
Add example of loading cell from a nib
Resolves #2. Thanks to @mbogh for helping put this together! There are still 2 outstanding issues, they seem like Apple bugs at this point: 1. Cells loaded from a nib do not size correctly in iOS 8 when the table view width is not 320pts (e.g. landscape iPhone, any iPad). (Programmatically loaded cells work fine.) 2. Changing the Dynamic Type slider while the app is running does not fire the UIContentSizeCategoryDidChangeNotification, meaning the app does not respond by updating its UI until it is killed and launched again.
1 parent ed1373b commit 0d7480e

File tree

5 files changed

+146
-22
lines changed

5 files changed

+146
-22
lines changed

TableViewCellWithAutoLayout.xcodeproj/project.pbxproj

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5A3B25361945F89500838EF4 /* NibTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3B25351945F89500838EF4 /* NibTableViewCell.swift */; };
11+
5A3B25381945F8AC00838EF4 /* NibTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5A3B25371945F8AC00838EF4 /* NibTableViewCell.xib */; };
1012
99BCDCA518008C0000B8E66B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99BCDCA418008C0000B8E66B /* Foundation.framework */; };
1113
99BCDCA718008C0000B8E66B /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99BCDCA618008C0000B8E66B /* CoreGraphics.framework */; };
1214
99BCDCA918008C0000B8E66B /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99BCDCA818008C0000B8E66B /* UIKit.framework */; };
@@ -35,6 +37,8 @@
3537
/* End PBXContainerItemProxy section */
3638

3739
/* Begin PBXFileReference section */
40+
5A3B25351945F89500838EF4 /* NibTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NibTableViewCell.swift; sourceTree = "<group>"; };
41+
5A3B25371945F8AC00838EF4 /* NibTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NibTableViewCell.xib; sourceTree = "<group>"; };
3842
99BCDCA118008C0000B8E66B /* TableViewCellWithAutoLayout.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TableViewCellWithAutoLayout.app; sourceTree = BUILT_PRODUCTS_DIR; };
3943
99BCDCA418008C0000B8E66B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
4044
99BCDCA618008C0000B8E66B /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@@ -156,12 +160,30 @@
156160
children = (
157161
A7C103201943F73C006A9720 /* TableViewCellWithAutoLayout-Bridging-Header.h */,
158162
A7F5DDAB19440B7B008E238B /* Model.swift */,
159-
A7C103211943F73D006A9720 /* TableViewCell.swift */,
160163
A7F5DDAD194417DC008E238B /* TableViewController.swift */,
164+
B13F6E9E1947D6DE00FF3B42 /* Programmatic */,
165+
B13F6E9F1947D6EB00FF3B42 /* Interface Builder */,
161166
);
162167
path = TableViewController;
163168
sourceTree = "<group>";
164169
};
170+
B13F6E9E1947D6DE00FF3B42 /* Programmatic */ = {
171+
isa = PBXGroup;
172+
children = (
173+
A7C103211943F73D006A9720 /* TableViewCell.swift */,
174+
);
175+
name = Programmatic;
176+
sourceTree = "<group>";
177+
};
178+
B13F6E9F1947D6EB00FF3B42 /* Interface Builder */ = {
179+
isa = PBXGroup;
180+
children = (
181+
5A3B25351945F89500838EF4 /* NibTableViewCell.swift */,
182+
5A3B25371945F8AC00838EF4 /* NibTableViewCell.xib */,
183+
);
184+
name = "Interface Builder";
185+
sourceTree = "<group>";
186+
};
165187
B147F1811867A715002B0C25 /* UIView+AutoLayout */ = {
166188
isa = PBXGroup;
167189
children = (
@@ -216,7 +238,7 @@
216238
isa = PBXProject;
217239
attributes = {
218240
CLASSPREFIX = RJ;
219-
LastUpgradeCheck = 0500;
241+
LastUpgradeCheck = 0600;
220242
ORGANIZATIONNAME = RobotJackalope;
221243
TargetAttributes = {
222244
99BCDCBB18008C0000B8E66B = {
@@ -248,6 +270,7 @@
248270
buildActionMask = 2147483647;
249271
files = (
250272
99BCDCAF18008C0000B8E66B /* InfoPlist.strings in Resources */,
273+
5A3B25381945F8AC00838EF4 /* NibTableViewCell.xib in Resources */,
251274
99BCDCB718008C0000B8E66B /* Images.xcassets in Resources */,
252275
);
253276
runOnlyForDeploymentPostprocessing = 0;
@@ -268,6 +291,7 @@
268291
buildActionMask = 2147483647;
269292
files = (
270293
B147F1841867A715002B0C25 /* UIView+AutoLayout.m in Sources */,
294+
5A3B25361945F89500838EF4 /* NibTableViewCell.swift in Sources */,
271295
A7F5DDAE194417DC008E238B /* TableViewController.swift in Sources */,
272296
A7F5DDAA194408D9008E238B /* AppDelegate.swift in Sources */,
273297
A7F5DDAC19440B7B008E238B /* Model.swift in Sources */,
@@ -317,7 +341,6 @@
317341
isa = XCBuildConfiguration;
318342
buildSettings = {
319343
ALWAYS_SEARCH_USER_PATHS = NO;
320-
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
321344
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
322345
CLANG_CXX_LIBRARY = "libc++";
323346
CLANG_ENABLE_MODULES = YES;
@@ -357,7 +380,6 @@
357380
isa = XCBuildConfiguration;
358381
buildSettings = {
359382
ALWAYS_SEARCH_USER_PATHS = NO;
360-
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
361383
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
362384
CLANG_CXX_LIBRARY = "libc++";
363385
CLANG_ENABLE_MODULES = YES;
@@ -425,7 +447,6 @@
425447
99BCDCD118008C0000B8E66B /* Debug */ = {
426448
isa = XCBuildConfiguration;
427449
buildSettings = {
428-
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
429450
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/TableViewCellWithAutoLayout.app/TableViewCellWithAutoLayout";
430451
FRAMEWORK_SEARCH_PATHS = (
431452
"$(SDKROOT)/Developer/Library/Frameworks",
@@ -448,7 +469,6 @@
448469
99BCDCD218008C0000B8E66B /* Release */ = {
449470
isa = XCBuildConfiguration;
450471
buildSettings = {
451-
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
452472
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/TableViewCellWithAutoLayout.app/TableViewCellWithAutoLayout";
453473
FRAMEWORK_SEARCH_PATHS = (
454474
"$(SDKROOT)/Developer/Library/Frameworks",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// NibTableViewCell.swift
3+
// TableViewCellWithAutoLayout
4+
//
5+
// Copyright (c) 2014 Morten Bøgh
6+
//
7+
8+
import UIKit
9+
10+
class NibTableViewCell: UITableViewCell
11+
{
12+
@IBOutlet var titleLabel: UILabel
13+
@IBOutlet var bodyLabel: UILabel
14+
15+
override func awakeFromNib()
16+
{
17+
super.awakeFromNib()
18+
19+
updateFonts()
20+
}
21+
22+
func updateFonts()
23+
{
24+
titleLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
25+
bodyLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption2)
26+
}
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6154.17" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6153.11"/>
5+
</dependencies>
6+
<objects>
7+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
8+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
9+
<tableViewCell contentMode="scaleToFill" ambiguous="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="CellIdentifier" rowHeight="65" id="Y7n-L6-IzP" customClass="NibTableViewCell" customModule="TableViewCellWithAutoLayout" customModuleProvider="target">
10+
<rect key="frame" x="0.0" y="0.0" width="320" height="65"/>
11+
<autoresizingMask key="autoresizingMask"/>
12+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Y7n-L6-IzP" id="2eC-KE-sJB">
13+
<autoresizingMask key="autoresizingMask"/>
14+
<subviews>
15+
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" verticalCompressionResistancePriority="1000" placeholderIntrinsicWidth="290" placeholderIntrinsicHeight="20" text="Title Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="e34-1u-xXd">
16+
<rect key="frame" x="15" y="10" width="290" height="20"/>
17+
<color key="backgroundColor" red="0.0" green="0.0" blue="1" alpha="0.10000000000000001" colorSpace="calibratedRGB"/>
18+
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
19+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
20+
<nil key="highlightedColor"/>
21+
</label>
22+
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" verticalCompressionResistancePriority="1000" placeholderIntrinsicWidth="290" placeholderIntrinsicHeight="14" text="Multiline body label." lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="290" translatesAutoresizingMaskIntoConstraints="NO" id="Yui-Bi-iHF">
23+
<rect key="frame" x="15" y="40" width="290" height="14"/>
24+
<color key="backgroundColor" red="1" green="0.0" blue="0.0" alpha="0.10000000000000001" colorSpace="calibratedRGB"/>
25+
<fontDescription key="fontDescription" style="UICTFontTextStyleCaption2"/>
26+
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
27+
<nil key="highlightedColor"/>
28+
</label>
29+
</subviews>
30+
<color key="backgroundColor" red="0.0" green="1" blue="0.0" alpha="0.10000000000000001" colorSpace="calibratedRGB"/>
31+
<constraints>
32+
<constraint firstItem="e34-1u-xXd" firstAttribute="top" secondItem="2eC-KE-sJB" secondAttribute="top" constant="10" id="1E0-WD-A4c"/>
33+
<constraint firstItem="Yui-Bi-iHF" firstAttribute="top" relation="greaterThanOrEqual" secondItem="e34-1u-xXd" secondAttribute="bottom" constant="10" id="OpD-Xj-NIW"/>
34+
<constraint firstItem="e34-1u-xXd" firstAttribute="leading" secondItem="2eC-KE-sJB" secondAttribute="leading" constant="15" id="Zx5-eL-3o4"/>
35+
<constraint firstAttribute="bottom" secondItem="Yui-Bi-iHF" secondAttribute="bottom" constant="10" id="aSL-wR-GoJ"/>
36+
<constraint firstAttribute="trailing" secondItem="Yui-Bi-iHF" secondAttribute="trailing" constant="15" id="hMY-NY-o4w"/>
37+
<constraint firstAttribute="trailing" secondItem="e34-1u-xXd" secondAttribute="trailing" constant="15" id="jRY-6O-qcD"/>
38+
<constraint firstItem="Yui-Bi-iHF" firstAttribute="leading" secondItem="2eC-KE-sJB" secondAttribute="leading" constant="15" id="mIC-l6-1u1"/>
39+
</constraints>
40+
</tableViewCellContentView>
41+
<connections>
42+
<outlet property="bodyLabel" destination="Yui-Bi-iHF" id="acr-ge-qlW"/>
43+
<outlet property="titleLabel" destination="e34-1u-xXd" id="9WJ-z8-rg8"/>
44+
</connections>
45+
<point key="canvasLocation" x="426" y="435.5"/>
46+
</tableViewCell>
47+
</objects>
48+
</document>

TableViewCellWithAutoLayout/TableViewController/TableViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TableViewCell: UITableViewCell
4040
contentView.addSubview(titleLabel)
4141
contentView.addSubview(bodyLabel)
4242

43-
contentView.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.1) // light red
43+
contentView.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.1) // light green
4444

4545
updateFonts()
4646
}

TableViewCellWithAutoLayout/TableViewController/TableViewController.swift

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import UIKit
99

10-
class TableViewController : UITableViewController
10+
class TableViewController: UITableViewController
1111
{
1212
let kCellIdentifier = "CellIdentifier"
1313

@@ -35,7 +35,18 @@ class TableViewController : UITableViewController
3535

3636
tableView.allowsSelection = false
3737

38-
tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
38+
39+
40+
/******************************************************************
41+
SWITCH BETWEEN PROGRAMMATIC AND INTERFACE BUILDER LOADED CELLS
42+
43+
Uncomment ONE of the two lines below to switch between approaches.
44+
Make sure that the other line commented out - don't uncomment both!
45+
*******************************************************************/
46+
tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: kCellIdentifier) // uncomment this line to load table view cells programmatically
47+
// tableView.registerNib(UINib(nibName: "NibTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kCellIdentifier) // uncomment this line to load table view cells from IB
48+
49+
3950

4051
// Self-sizing table view cells in iOS 8 require that the rowHeight property of the table view be set to the constant UITableViewAutomaticDimension
4152
tableView.rowHeight = UITableViewAutomaticDimension
@@ -100,19 +111,37 @@ class TableViewController : UITableViewController
100111

101112
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
102113
{
103-
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as TableViewCell
104-
105-
// Configure the cell for this indexPath
106-
cell.updateFonts()
107-
let modelItem = model.dataArray[indexPath.row]
108-
cell.titleLabel.text = modelItem.title
109-
cell.bodyLabel.text = modelItem.body
110-
111-
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
112-
cell.setNeedsUpdateConstraints()
113-
cell.updateConstraintsIfNeeded()
114-
115-
return cell
114+
// This will be the case for programmatically loaded cells (see viewDidLoad to switch approaches)
115+
if let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? TableViewCell {
116+
// Configure the cell for this indexPath
117+
cell.updateFonts()
118+
let modelItem = model.dataArray[indexPath.row]
119+
cell.titleLabel.text = modelItem.title
120+
cell.bodyLabel.text = modelItem.body
121+
122+
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
123+
cell.setNeedsUpdateConstraints()
124+
cell.updateConstraintsIfNeeded()
125+
126+
return cell
127+
}
128+
129+
// This will be the case for interface builder loaded cells (see viewDidLoad to switch approaches)
130+
if let cell: NibTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? NibTableViewCell {
131+
// Configure the cell for this indexPath
132+
cell.updateFonts()
133+
let modelItem = model.dataArray[indexPath.row]
134+
cell.titleLabel.text = modelItem.title
135+
cell.bodyLabel.text = modelItem.body
136+
137+
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
138+
cell.setNeedsUpdateConstraints()
139+
cell.updateConstraintsIfNeeded()
140+
141+
return cell
142+
}
143+
144+
return nil
116145
}
117146

118147
/*

0 commit comments

Comments
 (0)