ボタンの追加
- 評価
- クリップ 0
- VIEW 941

退会済みユーザー
実現したいこと
上の手書きで書いたイメージ図みたいにadd sectionでセクションを追加した時にsectionと他にその下にrowを追加できるボタンを備え付けたいです
そしてrowをタップ時にアラートテキストで入力できるようにしたいです
試してみたこと
自分ではとあるgithubを参考にしてsectionを追加するボタンと削除できるボタンを作ったのですがrowを追加するボタンの作り方が分からなくて色々試してみたのですがどうしても思いつかないので教えていただけたら嬉しいです。
//編集・追加
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var sectionTitleArray = [String]()
var dataArray1 = ["add Row"]
var dataArrayGroup: [[DataModel]] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
tableView.tableFooterView = UIView()
}
@IBAction func addSection(_ sender: UIButton) {
let alert = UIAlertController(title:"Sction追加",
message: "SectionTitleを入力してください",
preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler:
{ action -> Void in
print("Cancel")
})
let defaultAction = UIAlertAction(title: "OK",
style: .default,
handler:
{ action -> Void in
// TextFieldから値を取得
if let textFields = alert.textFields {
for textField in textFields {
if let text = textField.text, !text.isEmpty {
// 取得したテキストをセクションのタイトルとして追加する
print(text)
self.sectionTitleArray.insert(text, at: 0)
self.dataArrayGroup.insert([], at: 0)
self.tableView.insertSections(IndexSet(integer: 0), with: .automatic)
}
}
}
})
alert.addAction(cancelAction)
alert.addAction(defaultAction)
alert.addTextField(configurationHandler: { text -> Void in })
present(alert, animated: true, completion: nil)
//addrowボタン
let count = dataArrayGroup[1].count
dataArray1[1].insert([], at: 0)
tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic)
}
@IBAction func deleteSection(_ sender: UIButton) {
if sectionTitleArray.isEmpty {
return
}
let alert = UIAlertController(title:"Sction削除",
message: "削除するSectionTitleを入力してください",
preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler:
{ action -> Void in
print("Cancel")
})
let defaultAction = UIAlertAction(title: "OK",
style: .default,
handler:
{ action -> Void in
// TextFieldから値を取得
if let textFields = alert.textFields {
for textField in textFields {
if let text = textField.text,
!text.isEmpty,
let index = self.sectionTitleArray.index(of: text) {
self.sectionTitleArray.remove(at: index)
self.dataArrayGroup.remove(at: index)
self.tableView.deleteSections(IndexSet(integer: index), with: .automatic)
}
}
}
})
alert.addAction(cancelAction)
alert.addAction(defaultAction)
alert.addTextField(configurationHandler: { text -> Void in })
present(alert, animated: true, completion: nil)
}
// Rowを追加する(アニメーションはご自由に)
@IBAction func addRow(_ sender: UIButton) {
//func addRow() {
if dataArrayGroup.count == 0 {
return
}
let alert = UIAlertController(title:"Row追加",
message: "個数と金額を入力してください",
preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler:
{ action -> Void in
print("Cancel")
})
let count = dataArrayGroup[0].count
let defaultAction = UIAlertAction(title: "OK",
style: .default,
handler:
{ action -> Void in
// TextFieldから値を取得
if let textFields = alert.textFields {
let data = DataModel()
for textField in textFields {
if let text = textField.text, !text.isEmpty, let _ = Int(text) {
if textField.tag == 1 {
data.count = text
} else {
data.price = text
}
}
}
self.dataArrayGroup[0].insert(data, at: count)
self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic)
}
})
alert.addTextField {
$0.placeholder = "個数"
$0.keyboardType = .numberPad
$0.tag = 1
}
alert.addTextField {
$0.placeholder = "金額"
$0.keyboardType = .numberPad
$0.tag = 2
}
alert.addAction(cancelAction)
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
}
// MARK: - TableView Delegate & DataSource
//この部分です。
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if sectionTitleArray.count == 0 {
return nil
} else {
return sectionTitleArray[section]
}
}
// Section Count
func numberOfSections(in tableView: UITableView) -> Int {
return dataArrayGroup.count
}
// Row Count
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArrayGroup[section].count
}
// Generate Cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.data = dataArrayGroup[indexPath.section][indexPath.row]
cell.indexLabel.text = String(indexPath.row + 1)
return cell
}
// Select Cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
dataArrayGroup[indexPath.section].remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
//cellが削除が可能なことを伝える
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.delete;
}
}
参考文献
KentarouさんのGitHub
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.10%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/03/10 18:06
アドバイスを参考にしてとりあえずsection追加時にrowも同時に一行追加できるようにしようと試みてみたのですがどうしてもうまく行かなかったのでもう少し具体的に教えていただけないでしょうか?
2017/05/29 00:40
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArrayGroup[section].count+1
すいませんマナー違反だと思うのすが再度どうしてもお聞きしたく質問させていただきました
rowの配列+1とはこのことでしょうか?また配列の最後のrowを追加とはどういうことでしょうか?
お手数ですが教えていただきたいです。
2017/05/31 08:05
ここ数日間自分なりに考えていろいろ工夫してみたのですがどうしてもうまく出来ないので教えて頂けたら幸いです。
ぜひお願いします。