回答編集履歴
1
追記しました
answer
CHANGED
@@ -60,4 +60,110 @@
|
|
60
60
|
|
61
61
|
...
|
62
62
|
}
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
[追記]
|
67
|
+
|
68
|
+
```
|
69
|
+
import UIKit
|
70
|
+
|
71
|
+
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
|
72
|
+
|
73
|
+
@IBOutlet weak var mytableView: UITableView!
|
74
|
+
var item = [String]()
|
75
|
+
|
76
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
77
|
+
return item.count
|
78
|
+
}
|
79
|
+
|
80
|
+
//アラート
|
81
|
+
func addalert() {
|
82
|
+
let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
|
83
|
+
|
84
|
+
// OKボタンの設定
|
85
|
+
let okAction = UIAlertAction(title: "OK", style: .default, handler: {
|
86
|
+
(action:UIAlertAction!) -> Void in
|
87
|
+
|
88
|
+
// OKを押した時入力されていたテキストを表示
|
89
|
+
if let textFields = alert.textFields {
|
90
|
+
|
91
|
+
// アラートに含まれるすべてのテキストフィールドを調べる
|
92
|
+
for textField in textFields {
|
93
|
+
|
94
|
+
self.item.insert(textField.text!, at: 0)
|
95
|
+
self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
|
96
|
+
print(textField.text!)
|
97
|
+
}
|
98
|
+
}
|
99
|
+
})
|
100
|
+
alert.addAction(okAction)
|
101
|
+
|
102
|
+
// キャンセルボタンの設定
|
103
|
+
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
104
|
+
alert.addAction(cancelAction)
|
105
|
+
|
106
|
+
// テキストフィールドを追加
|
107
|
+
alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
|
108
|
+
textField.placeholder = "テキスト"
|
109
|
+
})
|
110
|
+
|
111
|
+
alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
|
112
|
+
|
113
|
+
// アラートを画面に表示
|
114
|
+
self.present(alert, animated: true, completion: nil)
|
115
|
+
}
|
116
|
+
|
117
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
118
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
119
|
+
let todoLabel = item[indexPath.row]
|
120
|
+
cell.textLabel?.text = todoLabel
|
121
|
+
|
122
|
+
//ボタンについて
|
123
|
+
let button = UIButton()
|
124
|
+
button.backgroundColor = UIColor.blue
|
125
|
+
button.setTitle(" 追 加 ", for: .normal)
|
126
|
+
cell.contentView.addSubview(button)
|
127
|
+
|
128
|
+
button.translatesAutoresizingMaskIntoConstraints = false
|
129
|
+
cell.contentView.rightAnchor.constraint(equalTo: button.rightAnchor, constant: 12).isActive = true
|
130
|
+
// 中央にする
|
131
|
+
//button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
|
132
|
+
//丸みに対して
|
133
|
+
button.layer.cornerRadius = 10
|
134
|
+
button.layer.masksToBounds = true
|
135
|
+
|
136
|
+
cell.contentView.heightAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true
|
137
|
+
|
138
|
+
//ラベルについて
|
139
|
+
let label = UILabel()
|
140
|
+
label.backgroundColor = UIColor.darkGray
|
141
|
+
|
142
|
+
if indexPath.row < item.count {
|
143
|
+
label.text = item[indexPath.row]
|
144
|
+
}
|
145
|
+
|
146
|
+
//丸みに対して
|
147
|
+
label.layer.cornerRadius = 5
|
148
|
+
label.layer.masksToBounds = true
|
149
|
+
cell.contentView.addSubview(label)
|
150
|
+
|
151
|
+
label.translatesAutoresizingMaskIntoConstraints = false
|
152
|
+
cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true
|
153
|
+
cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true
|
154
|
+
|
155
|
+
return cell
|
156
|
+
}
|
157
|
+
|
158
|
+
@IBAction func addlabel(_ sender: Any) {
|
159
|
+
addalert()
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
override func viewDidLoad() {
|
164
|
+
super.viewDidLoad()
|
165
|
+
mytableView.dataSource = self
|
166
|
+
mytableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
|
167
|
+
}
|
168
|
+
}
|
63
169
|
```
|