swift
1struct Cell { 2 var detail: [String] 3 var lightswipe: Bool//detail 4} 5 6import UIKit 7 8class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 9 10 @IBOutlet weak var mytableView: UITableView! 11 12 //構造体 13 var titles: [String] = [] // title用 14 var products: [Cell] = []//detail 15 16 17 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 18 // 全データの合計 19 return titles.count + products.count 20 } 21 22 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 23 let cell = UITableViewCell() 24 let index = Int(floor(Double(indexPath.row / 2))) 25 //セルタップ時のハイライトについて 26 cell.selectionStyle = .none 27 // title行 28 if indexPath.row % 2 == 0 { 29 30 guard titles.count > 0 else { 31 return cell 32 } 33 34 35 let title = titles[index] 36 cell.textLabel?.text = title 37 //センター寄せ 38 cell.textLabel!.textAlignment = NSTextAlignment.center 39 cell.backgroundColor = UIColor(red: 0.0, green: 0.8, blue: 1.0, alpha: 0.3) 40 41 // detail行 42 } else { 43 cell.backgroundColor = UIColor.lightGray 44 let item = products[index] 45 46 guard item.detail.count > 0 else { 47 return cell 48 } 49 50 var labels: [UILabel] = [] 51 52 //labelについて 53 for i in item.detail { 54 let label = UILabel() 55 label.backgroundColor = UIColor(red: 0.0, green: 0.8, blue: 0.9, alpha: 0.2) 56 label.text = i 57 58 59 // textLabel という名前の変数に格納された UILabel にフォントサイズの自動調整を設定します。 60 label.adjustsFontSizeToFitWidth = true 61 label.minimumScaleFactor = 10.0 62 //丸みに対して 63 label.layer.cornerRadius = 5 64 label.layer.masksToBounds = true 65 cell.contentView.addSubview(label) 66 labels.append(label) 67 } 68 69 var preLabel: UILabel? = nil 70 for l in labels { 71 72 l.translatesAutoresizingMaskIntoConstraints = false 73 if (preLabel == nil) { 74 l.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 12).isActive = true 75 } else { 76 l.leftAnchor.constraint(equalTo: preLabel!.rightAnchor, constant: 20).isActive = true 77 } 78 cell.contentView.heightAnchor.constraint(equalTo: l.heightAnchor, multiplier: 1).isActive = true 79 preLabel = l 80 } 81 82 } 83 84 return cell 85 } 86 87 88 @IBAction func addcellbtr(_ sender: Any) { 89 90 alertnormal() 91 } 92 93 //cellの削除について 94 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 95 //return products[indexPath.row].detail ? .delete : .none 96 if mytableView.isEditing { 97 return .delete 98 } 99 return .none 100 } 101 //cellの削除について 102 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 103 if editingStyle == .delete { 104 self.deleteRow(at: indexPath) 105 } 106 } 107 108 func deleteRow(at indexPath: IndexPath) { 109 110 self.titles.remove(at: indexPath.row) 111 mytableView.deleteRows(at: [indexPath], with: .fade)// 112 } 113 114 override func viewDidLoad() { 115 super.viewDidLoad() 116 mytableView.delegate = self 117 mytableView.dataSource = self 118 mytableView.reloadData() 119 } 120 121 122 123 override func didReceiveMemoryWarning() 124}
やりたいこと
titleとなるセルとdetailとなるセルをセットにしてセルをデリート出来る様にしたい。
出来たこと
titileとなるセルはデリートすることが出来た。
self.titles.remove(at: indexPath.row)
困っていること
titleとなるセルとdetailとなるセルを同時にデリートしたいが下記コードを追加するとIndex out of rangeが起きてしまう。エラーコードの意味は理解しているがどうコーディングすればいいか分からなくて困っています。
self.detail.remove(at: indexPath.row)
またdetailセルはtitleセルの下のセルなので下記の様にもしてみたが出来ませんでした。
self.titles.remove(at: indexPath.row + 1)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。