Skip to content

Commit d771856

Browse files
committed
Add TextField with Title.
1 parent 5ce639a commit d771856

7 files changed

+178
-18
lines changed

SPDiffable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPDiffable'
4-
s.version = '4.0.7'
4+
s.version = '4.0.8'
55
s.summary = 'Extension of Diffable API which allow not duplicate code and use less models. Included example for SideBar.'
66
s.homepage = 'https://github.com/ivanvorobei/SPDiffable'
77
s.source = { :git => 'https://github.com/ivanvorobei/SPDiffable.git', :tag => s.version }

Sources/SPDiffable/Table/Cells/SPDiffableTextFieldTableViewCell.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ import UIKit
2323

2424
open class SPDiffableTextFieldTableViewCell: UITableViewCell {
2525

26+
// MARK: - Data
27+
2628
public static var reuseIdentifier: String { "SPDiffableTextFieldTableViewCell" }
2729

30+
// MARK: - Views
31+
2832
public let textField = UITextField()
2933

34+
// MARK: - Init
35+
3036
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
3137
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
3238
commonInit()
@@ -48,6 +54,21 @@ open class SPDiffableTextFieldTableViewCell: UITableViewCell {
4854
detailTextLabel?.text = nil
4955
}
5056

57+
open func configure(with item: SPDiffableTableRowTextField) {
58+
imageView?.image = item.icon
59+
textField.text = item.text
60+
textField.placeholder = item.placeholder
61+
textField.autocorrectionType = item.autocorrectionType
62+
textField.keyboardType = item.keyboardType
63+
textField.autocapitalizationType = item.autocapitalizationType
64+
textField.delegate = item.delegate
65+
textField.clearButtonMode = item.clearButtonMode
66+
accessoryView = .none
67+
selectionStyle = .none
68+
}
69+
70+
// MARK: - Layout
71+
5172
open override func layoutSubviews() {
5273
super.layoutSubviews()
5374
textField.frame = .init(
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by)
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
24+
open class SPDiffableTextFieldTitleTableViewCell: UITableViewCell {
25+
26+
// MARK: - Data
27+
28+
public static var reuseIdentifier: String { "SPDiffableTextFieldTitleTableViewCell" }
29+
30+
// MARK: - Views
31+
32+
public let textField = UITextField()
33+
34+
// MARK: - Init
35+
36+
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
37+
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
38+
commonInit()
39+
}
40+
41+
public required init?(coder: NSCoder) {
42+
super.init(coder: coder)
43+
commonInit()
44+
}
45+
46+
private func commonInit() {
47+
textField.backgroundColor = .clear
48+
contentView.addSubview(textField)
49+
}
50+
51+
open override func prepareForReuse() {
52+
super.prepareForReuse()
53+
accessoryView = nil
54+
detailTextLabel?.text = nil
55+
}
56+
57+
open func configure(with item: SPDiffableTableRowTextFieldTitle) {
58+
imageView?.image = item.icon
59+
textLabel?.text = item.title
60+
textField.text = item.text
61+
textField.placeholder = item.placeholder
62+
textField.autocorrectionType = item.autocorrectionType
63+
textField.keyboardType = item.keyboardType
64+
textField.autocapitalizationType = item.autocapitalizationType
65+
textField.delegate = item.delegate
66+
textField.clearButtonMode = item.clearButtonMode
67+
textField.textAlignment = .right
68+
accessoryView = .none
69+
selectionStyle = .none
70+
}
71+
72+
// MARK: - Layout
73+
74+
open override func layoutSubviews() {
75+
super.layoutSubviews()
76+
if let textLabel = self.textLabel {
77+
let textFieldX = textLabel.frame.origin.x + textLabel.frame.width
78+
textField.frame = .init(
79+
x: textFieldX,
80+
y: .zero,
81+
width: contentView.frame.width - textFieldX - contentView.layoutMargins.right,
82+
height: contentView.frame.height
83+
)
84+
}
85+
86+
}
87+
88+
open override func sizeThatFits(_ size: CGSize) -> CGSize {
89+
let superSize = super.sizeThatFits(size)
90+
return .init(width: superSize.width, height: superSize.height + 4)
91+
}
92+
}

Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource+CellProvider.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ extension SPDiffableTableDataSource {
3636

3737
// MARK: - Ready Use
3838

39-
39+
4040
public static var `default`: [CellProvider] {
41-
#if os(iOS)
41+
#if os(iOS)
4242
return [rowDetail, rowSubtitle, `switch`, stepper, textField]
43-
#else
43+
#else
4444
return [rowDetail, rowSubtitle, textField]
45-
#endif
45+
#endif
4646
}
4747

4848
public static var rowDetail: CellProvider {
@@ -93,7 +93,7 @@ extension SPDiffableTableDataSource {
9393
}
9494
}
9595
#endif
96-
96+
9797
#if os(iOS)
9898
public static var stepper: CellProvider {
9999
return CellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
@@ -125,18 +125,18 @@ extension SPDiffableTableDataSource {
125125

126126
public static var textField: CellProvider {
127127
return CellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
128-
guard let item = item as? SPDiffableTableRowTextField else { return nil }
129-
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTextFieldTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTextFieldTableViewCell
130-
cell.textField.text = item.text
131-
cell.textField.placeholder = item.placeholder
132-
cell.textField.autocorrectionType = item.autocorrectionType
133-
cell.textField.keyboardType = item.keyboardType
134-
cell.textField.autocapitalizationType = item.autocapitalizationType
135-
cell.textField.delegate = item.delegate
136-
cell.textField.clearButtonMode = item.clearButtonMode
137-
cell.accessoryView = .none
138-
cell.selectionStyle = .none
139-
return cell
128+
switch item {
129+
case let item as SPDiffableTableRowTextFieldTitle:
130+
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTextFieldTitleTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTextFieldTitleTableViewCell
131+
cell.configure(with: item)
132+
return cell
133+
case let item as SPDiffableTableRowTextField :
134+
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTextFieldTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTextFieldTableViewCell
135+
cell.configure(with: item)
136+
return cell
137+
default:
138+
return nil
139+
}
140140
}
141141
}
142142
}

Sources/SPDiffable/Table/Models/SPDiffableTableRowTextField.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import UIKit
2323

2424
open class SPDiffableTableRowTextField: SPDiffableItem {
2525

26+
open var icon: UIImage?
2627
open var text: String?
2728
open var placeholder: String
2829
open var autocorrectionType: UITextAutocorrectionType
@@ -33,6 +34,7 @@ open class SPDiffableTableRowTextField: SPDiffableItem {
3334

3435
public init(
3536
id: String,
37+
icon: UIImage? = nil,
3638
text: String?,
3739
placeholder: String,
3840
autocorrectionType: UITextAutocorrectionType,
@@ -41,6 +43,7 @@ open class SPDiffableTableRowTextField: SPDiffableItem {
4143
clearButtonMode: UITextField.ViewMode,
4244
delegate: UITextFieldDelegate?
4345
) {
46+
self.icon = icon
4447
self.text = text
4548
self.placeholder = placeholder
4649
self.delegate = delegate
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by)
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
24+
open class SPDiffableTableRowTextFieldTitle: SPDiffableTableRowTextField {
25+
26+
open var title: String
27+
28+
public init(
29+
id: String,
30+
icon: UIImage? = nil,
31+
title: String,
32+
text: String?,
33+
placeholder: String,
34+
autocorrectionType: UITextAutocorrectionType,
35+
keyboardType: UIKeyboardType,
36+
autocapitalizationType: UITextAutocapitalizationType,
37+
clearButtonMode: UITextField.ViewMode,
38+
delegate: UITextFieldDelegate?
39+
) {
40+
self.title = title
41+
super.init(id: id, icon: icon, text: text, placeholder: placeholder, autocorrectionType: autocorrectionType, keyboardType: keyboardType, autocapitalizationType: autocapitalizationType, clearButtonMode: clearButtonMode, delegate: delegate)
42+
}
43+
}

Sources/SPDiffable/Table/SPDiffableTableController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ open class SPDiffableTableController: UITableViewController {
3434
tableView.register(SPDiffableTableViewCell.self, forCellReuseIdentifier: SPDiffableTableViewCell.reuseIdentifier)
3535
tableView.register(SPDiffableSubtitleTableViewCell.self, forCellReuseIdentifier: SPDiffableSubtitleTableViewCell.reuseIdentifier)
3636
tableView.register(SPDiffableTextFieldTableViewCell.self, forCellReuseIdentifier: SPDiffableTextFieldTableViewCell.reuseIdentifier)
37+
tableView.register(SPDiffableTextFieldTitleTableViewCell.self, forCellReuseIdentifier: SPDiffableTextFieldTitleTableViewCell.reuseIdentifier)
3738
}
3839

3940
// MARK: - Configure

0 commit comments

Comments
 (0)