Skip to content

Commit e74d420

Browse files
committed
Added text field.
1 parent 74d5aa5 commit e74d420

4 files changed

+134
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 SPDiffableTextFieldTableViewCell: UITableViewCell {
25+
26+
public static var reuseIdentifier: String { "SPDiffableTextFieldTableViewCell" }
27+
28+
public let textField = UITextField()
29+
30+
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
31+
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
32+
commonInit()
33+
}
34+
35+
public required init?(coder: NSCoder) {
36+
super.init(coder: coder)
37+
commonInit()
38+
}
39+
40+
private func commonInit() {
41+
textField.backgroundColor = .clear
42+
contentView.addSubview(textField)
43+
}
44+
45+
open override func prepareForReuse() {
46+
super.prepareForReuse()
47+
accessoryView = nil
48+
detailTextLabel?.text = nil
49+
}
50+
51+
open override func layoutSubviews() {
52+
super.layoutSubviews()
53+
textField.frame = .init(
54+
x: contentView.layoutMargins.left,
55+
y: .zero,
56+
width: contentView.frame.width - contentView.layoutMargins.left - contentView.layoutMargins.right,
57+
height: contentView.frame.height
58+
)
59+
}
60+
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
/**
25+
SPDiffable: Table item with text field.
26+
*/
27+
open class SPDiffableTableRowTextField: SPDiffableItem {
28+
29+
open var text: String?
30+
open var placeholder: String
31+
open var autocorrectionType: UITextAutocorrectionType
32+
open var keyboardType: UIKeyboardType
33+
open var autocapitalizationType: UITextAutocapitalizationType
34+
open weak var delegate: UITextFieldDelegate?
35+
36+
public init(
37+
id: String,
38+
text: String?,
39+
placeholder: String,
40+
autocorrectionType: UITextAutocorrectionType,
41+
keyboardType: UIKeyboardType,
42+
autocapitalizationType: UITextAutocapitalizationType,
43+
delegate: UITextFieldDelegate?
44+
) {
45+
self.text = text
46+
self.placeholder = placeholder
47+
self.delegate = delegate
48+
self.autocorrectionType = autocorrectionType
49+
self.keyboardType = keyboardType
50+
self.autocapitalizationType = autocapitalizationType
51+
super.init(id: id)
52+
}
53+
}

Sources/SPDiffable/Table/Providers/SPDiffableTableCellProvider.swift

+18-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ open class SPDiffableTableCellProvider {
3636
// MARK: - Ready Use
3737

3838
public static var `default`: [SPDiffableTableCellProvider] {
39-
[rowDetail, rowSubtitle, `switch`, stepper]
39+
[rowDetail, rowSubtitle, `switch`, stepper, textField]
4040
}
4141

4242
public static var rowDetail: SPDiffableTableCellProvider {
@@ -95,4 +95,21 @@ open class SPDiffableTableCellProvider {
9595
return cell
9696
}
9797
}
98+
99+
public static var textField: SPDiffableTableCellProvider {
100+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
101+
guard let item = item as? SPDiffableTableRowTextField else { return nil }
102+
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTextFieldTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTextFieldTableViewCell
103+
print("reloaded item \(item.id)")
104+
cell.textField.text = item.text
105+
cell.textField.placeholder = item.placeholder
106+
cell.textField.autocorrectionType = item.autocorrectionType
107+
cell.textField.keyboardType = item.keyboardType
108+
cell.textField.autocapitalizationType = item.autocapitalizationType
109+
cell.textField.delegate = item.delegate
110+
cell.accessoryView = .none
111+
cell.selectionStyle = .none
112+
return cell
113+
}
114+
}
98115
}

Sources/SPDiffable/Table/SPDiffableTableController.swift

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ open class SPDiffableTableController: UITableViewController {
3939
tableView.delaysContentTouches = false
4040
tableView.register(SPDiffableTableViewCell.self, forCellReuseIdentifier: SPDiffableTableViewCell.reuseIdentifier)
4141
tableView.register(SPDiffableSubtitleTableViewCell.self, forCellReuseIdentifier: SPDiffableSubtitleTableViewCell.reuseIdentifier)
42+
tableView.register(SPDiffableTextFieldTableViewCell.self, forCellReuseIdentifier: SPDiffableTextFieldTableViewCell.reuseIdentifier)
4243
}
4344

4445
// MARK: - Init

0 commit comments

Comments
 (0)