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にしたいが構造体の中にあるので書き換えれなく困っています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/17 15:59