経緯
Take One さんの回答により第1画面より遷移する第2画面のラベルを一段より2段にすることができました。その第2画面に第1画面で選択した行の項目を第2画面のタイトルとして表示することは成功したのですが、そのラベルの幅を調整する方法を教えてください。
作りたい画面:
第1図:第1画面 TableView01 完成
第2図:第2画面 TableView02 に第1画面で選んだ行に相当するタイトルをつけたが黒帯の幅を調整したい。
発生している問題
TableView01よりTableView02に遷移して表や表のタイトルを表示させることはお陰様で成功しております。しかし、上記第2図のとおり表示された黒帯の幅を調整しようとしても、コードの数字を変えても修正されません。
タイトルを表示するTableView02のコードは下記のとおりです。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let label = UILabel(frame: CGRect(x:0, y:150, width: tableView.bounds.width, height: 100)) //位置・サイズが無効 何故か??????????? // 文字位置 label.textAlignment = NSTextAlignment.Center // 文字サイズ label.font = UIFont.boldSystemFontOfSize(28) // 背景色 label.backgroundColor = UIColor.blackColor() // 文字色 label.textColor = UIColor.whiteColor() //表示される文字 if selectedIndexPathInViewController.row == 0 { label.text = "動画:動画の見方" } else if selectedIndexPathInViewController.row == 1 { label.text = "動画:電話" } else if selectedIndexPathInViewController.row == 2 { label.text = "動画:カレンダー" } else if selectedIndexPathInViewController.row == 3 { label.text = "動画:カメラ・ビデオ" } else if selectedIndexPathInViewController.row == 4 { label.text = "動画:テキストの見方" } else if selectedIndexPathInViewController.row == 5 { label.text = "テキスト:テキスト見方" } else if selectedIndexPathInViewController.row == 6 { label.text = "テキスト:初心者の頁" } else if selectedIndexPathInViewController.row == 7 { label.text = "テキスト:用語の説明" } else if selectedIndexPathInViewController.row == 8 { label.text = "テキスト:応援者の頁" } else { label.text = "?????" } // return label } //表のlabel1及びlabel2の文字を表示させる func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { return texts[selectedIndexPathInViewController.row].count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //teratailTake One さんのコメントアドバイスにより修正 //var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? CustomCell let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell cell.label1.text = texts[selectedIndexPathInViewController.row][indexPath.row] //一段目のために選ばれた行番号をプリントする print("タップされた 1段用 インデックスパス:\(indexPath.row)") cell.label2.text = subtexts[selectedIndexPathInViewController.row][indexPath.row] //二段目のために選ばれた行番号をプリントする print("タップされた 2段用 インデックスパス:\(indexPath.row)") return cell }
上記コードで 文字位置 文字サイズ 背景色 文字色はコードどおりに表示されます。しかし、コードの最初で設定したlabel位置およびサイズはコードの数字を変えても反映されません。
なお、第2画面のセルのlabel1およびlabel2の幅と位置はCustomCell.xibで設定をしています。
ご参考事項
AAA
iMac27 OS-X11.4 El Capitan
Xcode 7.3.1
Swift2
BBB
前の質問で教えて頂いた上下2枚のlabelを表示する第2画面は上記の第2図のとおりできました。改めて御礼申し上げます。下段の文字数が長いときは自動で改行もされます。これはCustomCell.xibでAutoLayoutを設定をしています。
CCC
その他、第3画面に遷移してWebViewを表示する方法について、別途質問をさせて頂きます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/16 13:17
2016/06/17 01:14