spreadsheetViewを使って表を作ってメモしていくアプリを作っています。
一通りアプリが完成して、シュミレーターを使って修正箇所を探しています。そこで、表の縦列を多くしてみようと思い、多くしたことろ、見出しに使っていた文字が、表に散らばっていくつも表示されてしまいました。そして、アクションをした時に、それは移動して違うところに散らばって表示されしまいます。
理由がわかりません。解決方法と、なぜそうなるのか教えていただけないでしょうか。
swift
1 2import UIKit 3import SpreadsheetView 4 5class ViewController: UIViewController, SpreadsheetViewDataSource, SpreadsheetViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource { 6 7 8 //spreadsheetView 9 @IBOutlet var spreadsheetView: SpreadsheetView! 10 //testScoretextField 11 @IBOutlet var testScoretextField: UITextField! 12 13 var subjectData: [String] = ["数学","国語","英語","社会","理科","物理","科学","化学","古文","現代文","英語2","数学2","国語2","体育","図工","社会2","理科2","物理2","科学2","化学2"] 14 15 var subjecttitle: [String] = ["教科","素点","結果点","平均"] 16 17 var subjectName: [String] = [] 18 19 var subjectNameCode: [String] = ["1"] 20 21 var score: [String] = ["0"] 22 23 var count = 0 24 25 var rowCount = 0 26 27 var type: Double = 6 28 29 30 //pickerViewのコード 31 32 //pickeViewの数 33 func numberOfComponents(in pickerView: UIPickerView) -> Int { 34 return 1 35 } 36 37 //pickeViewの中の行の数 38 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 39 return subjectData.count 40 } 41 42 //pickeViewの中に表示するデータ 43 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 44 return subjectData[row] 45 } 46 47 //pickeViewで選択されたのを取り出す 48 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 49 50 rowCount = row 51 52 53 54 } 55 56 //アプリを起動して画面を開いたら。っていうコード 57 override func viewDidLoad() { 58 // Do any additional setup after loading the view. 59 60 //ここからしたはまるパクリでOK 61 super.viewDidLoad() 62 spreadsheetView.register(MyLabelCell.self, forCellWithReuseIdentifier: MyLabelCell.identifier) 63 spreadsheetView.delegate = self 64 spreadsheetView.dataSource = self 65 view.addSubview(spreadsheetView) 66 67 testScoretextField.placeholder = "点数を入力" 68 } 69 70 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 71 72 self.spreadsheetView.reloadData() 73 score += [testScoretextField.text!] 74 subjectName = [] 75 subjectName = [subjectData[rowCount]] 76 testScoretextField.text = "" 77 if let firstIndex = subjectData.firstIndex(of: subjectName[0]) { 78 print("インデックス番号: (firstIndex)") 79 subjectNameCode += ["(firstIndex + 1)"] 80 count += 1 81 } 82 view.endEditing(true) 83 } 84 85 86 87 88 89 func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int { 90 return 4 91 } 92 93 func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int { 94 let subjectDatacount = Int(subjectData.count) + 1 95 return subjectDatacount 96 } 97 98 func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat { 99 return 72 100 101 102 103 } 104 105 func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat { 106 return 35 107 } 108 109 func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? { 110 var i = 0 111 let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: MyLabelCell.identifier, for: indexPath) as! MyLabelCell 112 113 114 115 if indexPath.section == 0 { 116 var i = 0 117 while (i <= subjectData.count){ 118 if indexPath.row == i + 1 { 119 cell.setup(with: String(subjectData[i])) 120 } 121 i += 1 122 } 123 } 124 125 if indexPath.row == 0 { 126 cell.setup(with: subjecttitle[indexPath.section]) 127 } 128 129 if indexPath.section == 1 { 130 while (i <= count){ 131 if indexPath.row == Int(subjectNameCode[i]) { 132 cell.setup(with: String(score[i])) 133 134 } 135 i += 1 136 } 137 } 138 139 if indexPath.section == 2 { 140 while (i <= count){ 141 if indexPath.row == Int(subjectNameCode[i]) { 142 cell.setup(with: String(atof(score[i]) * type / 10)) 143 144 } 145 i += 1 146 } 147 } 148 149 150 151 152 return cell 153 } 154 155 156 157 class MyLabelCell: Cell { 158 static let identifier = "MyLablelCell" 159 160 private let label = UILabel() 161 162 public func setup(with text: String) { 163 label.text = text 164 label.textAlignment = .center 165 contentView.addSubview(label) 166 } 167 168 override func layoutSubviews() { 169 super.layoutSubviews() 170 label.frame = contentView.bounds 171 } 172 } 173 174 175 176 177 178} 179 180
申し訳ありません。どこを切り取れば良いかわからなかったので、全てコードを書かせていただきました。申し訳ありません。
あなたの回答
tips
プレビュー