fix autolayout issues

This commit is contained in:
Max Sokolov 2016-05-25 01:31:22 +03:00
parent bba29151ec
commit 92ee2b731c
9 changed files with 131 additions and 22 deletions

View File

@ -27,10 +27,11 @@ public protocol RowBuilder {
var reusableIdentifier: String { get }
var numberOfRows: Int { get }
var estimatedRowHeight: CGFloat { get }
func willUpdateDirector(director: TableDirector?)
func rowHeight(index: Int) -> CGFloat
func estimatedRowHeight() -> CGFloat
func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject?
}

View File

@ -141,11 +141,10 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
// MARK: UITableViewDelegate - actions
public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return builderAtIndexPath(indexPath).0.estimatedRowHeight
return builderAtIndexPath(indexPath).0.estimatedRowHeight()
}
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let builder = builderAtIndexPath(indexPath)
return builder.0.rowHeight(builder.1)
}

View File

@ -37,11 +37,7 @@ public class TableBaseRowBuilder<DataType, CellType where CellType: UITableViewC
public var numberOfRows: Int {
return items.count
}
public var estimatedRowHeight: CGFloat {
return 44
}
public init(item: DataType, id: String? = nil) {
reusableIdentifier = id ?? String(CellType)
@ -61,6 +57,10 @@ public class TableBaseRowBuilder<DataType, CellType where CellType: UITableViewC
return UITableViewAutomaticDimension
}
public func estimatedRowHeight() -> CGFloat {
return 44
}
// MARK: - Chaining actions -
public func action(key: String, handler: (data: ActionData<DataType, CellType>) -> Void) -> Self {
@ -127,10 +127,6 @@ public class TableBaseRowBuilder<DataType, CellType where CellType: UITableViewC
*/
public class TableRowBuilder<DataType, CellType: ConfigurableCell where CellType.T == DataType, CellType: UITableViewCell> : TableBaseRowBuilder<DataType, CellType> {
public override var estimatedRowHeight: CGFloat {
return CGFloat(CellType.estimatedHeight())
}
public init(item: DataType) {
super.init(item: item, id: CellType.reusableIdentifier())
}
@ -146,13 +142,17 @@ public class TableRowBuilder<DataType, CellType: ConfigurableCell where CellType
}
return super.invoke(action: action, cell: cell, indexPath: indexPath, itemIndex: itemIndex, userInfo: userInfo)
}
public override func estimatedRowHeight() -> CGFloat {
return CGFloat(CellType.estimatedHeight())
}
}
public class TablePrototypeRowBuilder<DataType: Hashable, CellType: ConfigurableCell where CellType.T == DataType, CellType: UITableViewCell> : TableBaseRowBuilder<DataType, CellType> {
private var cachedHeights = [Int: CGFloat]()
private var prototypeCell: CellType?
public init(item: DataType) {
super.init(item: item, id: CellType.reusableIdentifier())
}
@ -160,6 +160,10 @@ public class TablePrototypeRowBuilder<DataType: Hashable, CellType: Configurable
public init(items: [DataType]? = nil) {
super.init(items: items, id: CellType.reusableIdentifier())
}
public override func estimatedRowHeight() -> CGFloat {
return UITableViewAutomaticDimension
}
public override func rowHeight(index: Int) -> CGFloat {
@ -168,17 +172,25 @@ public class TablePrototypeRowBuilder<DataType: Hashable, CellType: Configurable
let item = items[index]
if let height = cachedHeights[item.hashValue] {
return height
//return height
}
cell.bounds = CGRectMake(0, 0, tableDirector?.tableView?.bounds.size.width ?? 0, cell.bounds.height)
cell.bounds = CGRectMake(0, 0, tableDirector?.tableView?.bounds.size.width ?? 0, cell.bounds.height)
cell.configure(item)
cell.setNeedsLayout()
cell.layoutIfNeeded()
let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height + 1
cachedHeights[item.hashValue] = height
print(tableDirector?.tableView?.bounds.size.width, cell.bounds.height, height)
return height
}

View File

@ -21,7 +21,7 @@ class MainController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let rows = TablePrototypeRowBuilder<String, StoryboardTableViewCell>(items: ["1"])
let rows = TablePrototypeRowBuilder<String, StoryboardImageTableViewCell>(items: ["1", "1", "1", "1"])
.action(.click) { [unowned self] e in
self.performSegueWithIdentifier("headerfooter", sender: nil)
}

View File

@ -0,0 +1,37 @@
//
// StoryboardImageTableViewCell.swift
// TabletDemo
//
// Created by Max Sokolov on 24/05/16.
// Copyright © 2016 Tablet. All rights reserved.
//
import UIKit
import Tablet
class StoryboardImageTableViewCell: UITableViewCell, ConfigurableCell {
typealias T = String
@IBOutlet var titleLabel: UILabel!
@IBOutlet var subtitleLabel: UILabel!
@IBOutlet var customImageView: UIImageView!
func configure(string: T) {
titleLabel.text = "Test"
subtitleLabel.text = "Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.Copyright © 2016 Tablet. All rights reserved.1"
}
static func estimatedHeight() -> Float {
return 140
}
override func layoutSubviews() {
super.layoutSubviews()
//contentView.layoutIfNeeded()
subtitleLabel.preferredMaxLayoutWidth = subtitleLabel.bounds.size.width
}
}

View File

@ -33,6 +33,8 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->

View File

@ -39,8 +39,54 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="StoryboardImageTableViewCell" rowHeight="141" id="IBY-tW-SgU" customClass="StoryboardImageTableViewCell" customModule="TabletDemo" customModuleProvider="target">
<rect key="frame" x="0.0" y="92" width="600" height="141"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="IBY-tW-SgU" id="UNZ-nz-200">
<rect key="frame" x="0.0" y="0.0" width="600" height="140"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="751" verticalHuggingPriority="751" horizontalCompressionResistancePriority="751" verticalCompressionResistancePriority="751" placeholderIntrinsicWidth="172" placeholderIntrinsicHeight="21" text="Title" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YnK-4H-0SS">
<rect key="frame" x="128" y="20" width="452" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="750" verticalHuggingPriority="750" placeholderIntrinsicWidth="172" placeholderIntrinsicHeight="18" text="Subtitle" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="oPO-9F-UcX">
<rect key="frame" x="128" y="44" width="452" height="18"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="il6-0T-Sfb">
<rect key="frame" x="20" y="20" width="100" height="100"/>
<constraints>
<constraint firstAttribute="width" priority="999" constant="100" id="Ryh-cg-Q69"/>
<constraint firstAttribute="height" priority="999" constant="100" id="cjb-Sw-psw"/>
</constraints>
</imageView>
</subviews>
<constraints>
<constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="oPO-9F-UcX" secondAttribute="bottom" constant="20" id="3wD-H5-3wL"/>
<constraint firstAttribute="trailing" secondItem="oPO-9F-UcX" secondAttribute="trailing" constant="20" id="4ns-lI-pTV"/>
<constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="il6-0T-Sfb" secondAttribute="bottom" priority="999" constant="20" id="CQQ-43-LmG"/>
<constraint firstItem="il6-0T-Sfb" firstAttribute="top" secondItem="UNZ-nz-200" secondAttribute="top" constant="20" id="F5Q-jP-iQ0"/>
<constraint firstItem="YnK-4H-0SS" firstAttribute="leading" secondItem="il6-0T-Sfb" secondAttribute="trailing" constant="8" id="Qwz-5H-QFk"/>
<constraint firstItem="oPO-9F-UcX" firstAttribute="leading" secondItem="il6-0T-Sfb" secondAttribute="trailing" constant="8" id="VNa-5R-BEy"/>
<constraint firstItem="il6-0T-Sfb" firstAttribute="leading" secondItem="UNZ-nz-200" secondAttribute="leading" constant="20" id="dIx-cj-H8P"/>
<constraint firstItem="YnK-4H-0SS" firstAttribute="top" secondItem="UNZ-nz-200" secondAttribute="top" constant="20" id="h4u-Nr-cGF"/>
<constraint firstItem="oPO-9F-UcX" firstAttribute="top" secondItem="YnK-4H-0SS" secondAttribute="bottom" constant="3" id="j6U-hX-Z6k"/>
<constraint firstAttribute="trailing" secondItem="YnK-4H-0SS" secondAttribute="trailing" constant="20" id="ulW-hU-AJG"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="customImageView" destination="il6-0T-Sfb" id="Ufi-6Y-Vuf"/>
<outlet property="subtitleLabel" destination="oPO-9F-UcX" id="RIK-1t-nVt"/>
<outlet property="titleLabel" destination="YnK-4H-0SS" id="ilA-7H-pq7"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="StoryboardTableViewCell" id="nE5-Y5-OFf" customClass="StoryboardTableViewCell" customModule="TabletDemo" customModuleProvider="target">
<rect key="frame" x="0.0" y="92" width="600" height="44"/>
<rect key="frame" x="0.0" y="233" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nE5-Y5-OFf" id="3yF-sl-yNq">
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
DA08A0531CF4E9B500BBF1F8 /* StoryboardImageTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA08A0521CF4E9B500BBF1F8 /* StoryboardImageTableViewCell.swift */; };
DAC2D5CA1C9D303E009E9C19 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC2D5C91C9D303E009E9C19 /* AppDelegate.swift */; };
DAC2D5CF1C9D30A7009E9C19 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAC2D5CD1C9D30A7009E9C19 /* Main.storyboard */; };
DAC2D5D01C9D30A7009E9C19 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAC2D5CE1C9D30A7009E9C19 /* LaunchScreen.storyboard */; };
@ -17,6 +18,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
DA08A0521CF4E9B500BBF1F8 /* StoryboardImageTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardImageTableViewCell.swift; sourceTree = "<group>"; };
DAB7EB271BEF787300D2AD5E /* TabletDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TabletDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
DAC2D5C91C9D303E009E9C19 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
DAC2D5CD1C9D30A7009E9C19 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
@ -39,11 +41,19 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
DA539C871CF50B1800368ACB /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
DAB7EB1E1BEF787300D2AD5E = {
isa = PBXGroup;
children = (
DAC2D5C61C9D2FE5009E9C19 /* Classes */,
DAC2D5CB1C9D3058009E9C19 /* Resources */,
DA539C871CF50B1800368ACB /* Frameworks */,
DAB7EB281BEF787300D2AD5E /* Products */,
);
sourceTree = "<group>";
@ -122,6 +132,7 @@
isa = PBXGroup;
children = (
DACB71771CC2D6ED00432BD3 /* StoryboardTableViewCell.swift */,
DA08A0521CF4E9B500BBF1F8 /* StoryboardImageTableViewCell.swift */,
);
path = Views;
sourceTree = "<group>";
@ -202,6 +213,7 @@
DACB71761CC2D63D00432BD3 /* MainController.swift in Sources */,
DAC2D5CA1C9D303E009E9C19 /* AppDelegate.swift in Sources */,
DACB717A1CC2D89D00432BD3 /* HeaderFooterController.swift in Sources */,
DA08A0531CF4E9B500BBF1F8 /* StoryboardImageTableViewCell.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -298,7 +310,7 @@
INFOPLIST_FILE = Resources/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.tablet.tablet;
PRODUCT_BUNDLE_IDENTIFIER = com.tablet.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
};
@ -313,7 +325,7 @@
INFOPLIST_FILE = Resources/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.tablet.tablet;
PRODUCT_BUNDLE_IDENTIFIER = com.tablet.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
};