質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

1448閲覧

構造体について(tableView)

退会済みユーザー

退会済みユーザー

総合スコア0

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/05/15 06:46

編集2018/05/16 10:15

swift

1//ViewController.swift 2import UIKit 3 4class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 5 6 @IBOutlet weak var mytableView: UITableView! 7 8 //構造体 9 let initialItems: [String] = [] 10 var products: [[Cell]] = [[]] 11 var cnt = 0 12 //以下の2つの変数は削除予定 13 var item: [String] = [] 14 var itemTotal: [[String]] = [[]] 15 16 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 if (products.count == 0 18 || products.count > 0 && products[0].count == 0) { 19 return 0 20 } else { 21 return products.count 22 } 23 } 24 25 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 26 let cell = UITableViewCell() 27 28 //let item1 = products[indexPath.row] 29 //cell.textLabel?.text = item1.title 30 31 let index = products.count - 1 - indexPath.row 32 let item = products[index]//itemTotal[index] 33 34 guard item.count > 0 else { 35 return cell 36 } 37 38 39 var labels: [UILabel] = [] 40 //labelについて 41 for i in item { 42 let label = UILabel() 43 label.backgroundColor = UIColor.darkGray 44 label.text = i //string型じゃないとダメらしい 52行目 Cannot assign value of type 'Cell' to type 'String?' 45 46 47 // textLabel という名前の変数に格納された UILabel にフォントサイズの自動調整を設定します。 48 label.adjustsFontSizeToFitWidth = true 49 label.minimumScaleFactor = 10.0 50 //丸みに対して 51 label.layer.cornerRadius = 5 52 label.layer.masksToBounds = true 53 cell.contentView.addSubview(label) 54 labels.append(label) 55 } 56 57 var preLabel: UILabel? = nil 58 for l in labels { 59 60 l.translatesAutoresizingMaskIntoConstraints = false 61 if (preLabel == nil) { 62 l.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 12).isActive = true 63 } else { 64 l.leftAnchor.constraint(equalTo: preLabel!.rightAnchor, constant: 20).isActive = true 65 } 66 cell.contentView.heightAnchor.constraint(equalTo: l.heightAnchor, multiplier: 1).isActive = true 67 preLabel = l 68 } 69 70 71 return cell 72 } 73 74 75 @IBAction func addcellbtr(_ sender: Any) { 76 77 alertnormal() 78 } 79 80 func alertnormal(){ 81 // テキストフィールド付きアラート表示 82 83 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 84 85 // OKボタンの設定 86 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 87 (action:UIAlertAction!) -> Void in 88 89 // OKを押した時入力されていたテキストを表示 90 if let textFields = alert.textFields { 91 92 // アラートに含まれるすべてのテキストフィールドを調べる 93 for textField in textFields { 94 let name = textField.text! 95 self.products.insert([Cell(title: name, detail: name, cellcolor: false)], at: 0) 96 print(textField.text!) 97 } 98 self.mytableView.reloadData() 99 } 100 }) 101 alert.addAction(okAction) 102 103 // キャンセルボタンの設定 104 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 105 alert.addAction(cancelAction) 106 107 // テキストフィールドを追加 108 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 109 textField.placeholder = "テキスト" 110 }) 111 112 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 113 114 // アラートを画面に表示 115 self.present(alert, animated: true, completion: nil) 116 117 } 118 119 120 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 121 122 let edit = UIContextualAction(style: .normal,title: "追加", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in 123 124 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 125 126 // OKボタンの設定 127 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 128 (action:UIAlertAction!) -> Void in 129 130 // OKを押した時入力されていたテキストを表示 131 if let textFields = alert.textFields { 132 133 // アラートに含まれるすべてのテキストフィールドを調べる 134 for textField in textFields { 135 let name = textField.text! 136 self.products.append([Cell(detail: name, cellcolor: false )]) 137 self.item.insert(textField.text!, at: 0) 138 self.products[self.cnt] = self.item //146行目 Cannot assign value of type '[String]' to type '[Cell]' 139 140 if self.item.count % 5 == 0, self.products[0].count > 1 { 141 self.cnt += 1 142 self.products.append([]) 143 self.item = [] 144 print(textField.text!) 145 //self.Gesture() 146 //self.doubleclic() 147 } 148 self.mytableView.reloadData() 149 } 150 } 151 }) 152 alert.addAction(okAction) 153 154 // キャンセルボタンの設定 155 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 156 alert.addAction(cancelAction) 157 158 // テキストフィールドを追加 159 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 160 textField.placeholder = "テキスト" 161 }) 162 163 164 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 165 166 // アラートを画面に表示 167 self.present(alert, animated: true, completion: nil) 168 print("edit") 169 170 success(true) 171 }) 172 173 edit.backgroundColor = .blue 174 175 return UISwipeActionsConfiguration(actions: [edit]) 176 } 177 178 179 180 // trueを返すことでCellのアクションを許可しています 181 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 182 183 if products.count > (indexPath.row + 1) { 184 return true 185 }else{ 186 return false 187 } 188 } 189 190 191 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 192 return 40 193 } 194 195 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 196 return indexPath 197 } 198 199 override func viewDidLoad() { 200 super.viewDidLoad() 201 mytableView.delegate = self 202 mytableView.dataSource = self 203 mytableView.estimatedRowHeight = 60 204 mytableView.backgroundColor = UIColor(red: 0.0, green: 0.8, blue: 1.0, alpha: 0.1) 205 206 for detail in item { 207 products.append([Cell(detail: detail, cellcolor: false)]) 208 } 209 mytableView.reloadData() 210 } 211 212 213 214 override func didReceiveMemoryWarning() { 215 super.didReceiveMemoryWarning() 216 // Dispose of any resources that can be recreated. 217 } 218 219 220} 221

swift

1//item.swift 2struct Cell { 3 let title: String 4 let detail: [String] 5 var cellcolor: Bool 6 7 init(detail: String,cellcolor: Bool) { 8 self.init(title: detail,detail: detail,cellcolor: cellcolor) 9 } 10 11 init(title: String,detail: String,cellcolor: Bool) { 12 self.title = title; 13 self.detail = [detail]; 14 self.cellcolor = cellcolor; 15} 16 17}

やりたいこと

titleとなるセルとdetailとなるセルを同時に挿入させたい。
titleとなるセルは追加時にアラートで入力した題名が反映されるようにしたい
detailとなるセルは最初は空の状態でスワイプ処理によってアラートで入力してものが反映されるようにしたい。

出来ていること
detailセルに関するスワイプ処理についてまでは出来た。

困っていること
コード的にはエラーはなく、実行しても落ちないのですがアラートで入力したものがセルに挿入されなくて困っている。
アラートはちゃんと呼び出せて入力したものがコンソールエリアに表示にされているのでアラートの処理の問題ではないと思われる。その為、cellForRowAt内に問題がると思われるが何が問題なのかが分からない。
題名となるセルはセクションでも良いではないかという指摘はあるかもしれないがスワイプ処理なども考え、今回はセル2行で出来るようにしたいと考えている。
52行目、146行目での構造体配列による型のエラーで困っています。
他には各箇所のitemをdetailにしたいが構造体の中にあるので書き換えれなく困っています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

セクションを使わないのでしたら下記のような感じでいかがでしょうか?
titleとなるセルとdetailとなるセルは別の配列でデータを保持した方が良いのかなと思います。

//ViewController.swift import UIKit // detail用構造体 struct Cell { var detail: [String] var cellcolor: Bool } class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ @IBOutlet weak var mytableView: UITableView! var titles: [String] = [] // title用 var products: [Cell] = [] // detail用 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // 全データの合計 return titles.count + products.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() let index = Int(floor(Double(indexPath.row / 2))) // title行 if indexPath.row % 2 == 0 { guard titles.count > 0 else { return cell } let title = titles[index] cell.textLabel?.text = title    // detail行 } else { let item = products[index] guard item.detail.count > 0 else { return cell } var labels: [UILabel] = [] //labelについて for i in item.detail { let label = UILabel() label.backgroundColor = UIColor.darkGray label.text = i // textLabel という名前の変数に格納された UILabel にフォントサイズの自動調整を設定します。 label.adjustsFontSizeToFitWidth = true label.minimumScaleFactor = 10.0 //丸みに対して label.layer.cornerRadius = 5 label.layer.masksToBounds = true cell.contentView.addSubview(label) labels.append(label) } var preLabel: UILabel? = nil for l in labels { l.translatesAutoresizingMaskIntoConstraints = false if (preLabel == nil) { l.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 12).isActive = true } else { l.leftAnchor.constraint(equalTo: preLabel!.rightAnchor, constant: 20).isActive = true } cell.contentView.heightAnchor.constraint(equalTo: l.heightAnchor, multiplier: 1).isActive = true preLabel = l } } return cell } @IBAction func addcellbtr(_ sender: Any) { alertnormal() } func alertnormal(){ // テキストフィールド付きアラート表示 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { // アラートに含まれるすべてのテキストフィールドを調べる for textField in textFields { let name = textField.text! self.titles.insert(name, at: 0) } self.products.insert(Cell(detail: [], cellcolor: false), at: 0) self.mytableView.reloadData() } }) alert.addAction(okAction) // キャンセルボタンの設定 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) // テキストフィールドを追加 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 // アラートを画面に表示 self.present(alert, animated: true, completion: nil) } func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let edit = UIContextualAction(style: .normal,title: "追加", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { // アラートに含まれるすべてのテキストフィールドを調べる for textField in textFields { let name = textField.text! let index = Int(floor(Double(indexPath.row / 2))) self.products[index].detail.append(name) self.mytableView.reloadData() } } }) alert.addAction(okAction) // キャンセルボタンの設定 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) // テキストフィールドを追加 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 // アラートを画面に表示 self.present(alert, animated: true, completion: nil) print("edit") success(true) }) edit.backgroundColor = .blue return UISwipeActionsConfiguration(actions: [edit]) } // trueを返すことでCellのアクションを許可しています func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // detail行の場合は許可 return indexPath.row % 2 == 1 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 40 } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath } override func viewDidLoad() { super.viewDidLoad() mytableView.delegate = self mytableView.dataSource = self mytableView.estimatedRowHeight = 60 mytableView.backgroundColor = UIColor(red: 0.0, green: 0.8, blue: 1.0, alpha: 0.1) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

投稿2018/05/16 22:06

newmt

総合スコア1277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/05/17 15:59

懇切に教えて頂きありがとうございます。一つお聞きしたいのですが別々にデータを保持するのと、一緒に保存するのでは何か違いがあるのでしょうか? また、ラベルを追加するごとに一列に収まらなくなった場合にセルの幅が拡張され、2行目に行くの仕様にしたい場合時に以下のコードを追加したのですが改行できませんでした。 この場合どの様にすればいいのでしょうか? self.estimatedRowHeight=50 self.rowHeight=UITableViewAutomaticDimension
guest

0

itemTotalという変数は何のためのものでしょう?
どこからも要素を追加していないのであれば、

swift

1func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 2 if (products.count == 0 3 || products.count > 0 && itemTotal[0].count == 0) { 4 return 0 5 } else { 6 return products.count 7 } 8}

ここでセルの数が0になってしまうのでtableViewには何も表示されません。

投稿2018/05/15 22:46

編集2018/05/15 22:50
kakajika

総合スコア3131

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/05/16 05:33

itemTotalは前のコードの変数で構造体への置き換え方が分からなくて今回のようなコードのになりました。 それとitemTotalはdetailセルの処理の中のラベルに関する処理です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問