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

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

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

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

Swift

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

Q&A

解決済

1回答

1749閲覧

Swiftのセクションタイトルがずれてしまうので直したい

sunglass

総合スコア303

Xcode

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

Swift

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

0グッド

0クリップ

投稿2020/10/05 02:46

編集2020/10/05 04:55

イメージ説明

画像のようにUITableViewのセクションだけ文字が左に寄っています。
(セルの方でpaddingのような処理はしていません。。)
何が原因でしょうか??

セクションタイトルのNewsをセルのtest...と同じ位置に修正したいです。

追記:カスタムセルは使っていません。。

import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var shadowView: UIView! @IBOutlet weak var tableHeight: NSLayoutConstraint! @IBOutlet weak var itemAddField: UITextField! let customUITextField:CustomTextField = CustomTextField() var title: String = "" var List = ["testtest", "testtest", "testtest", "testtest", ] let sectionTitles = ["News"] func numberOfSections(in tableView: UITableView) -> Int { return sectionTitles.count } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 48 } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sectionTitles[section] } func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { view.tintColor = .blue let header = view as! UITableViewHeaderFooterView header.textLabel?.textColor = .white } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return List.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = List[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.title = List[indexPath.row] let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "navigationPushView") as! SecondViewController nextVC.data = self.title self.navigationController?.pushViewController(nextVC, animated: true) // セルの選択を解除 tableView.deselectRow(at: indexPath, animated: true) } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } //スワイプしたセルを削除 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == UITableViewCell.EditingStyle.delete { List.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic) } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // for textField itemAddField.layer.masksToBounds = false itemAddField.layer.shadowRadius = 5.0 itemAddField.layer.shadowColor = UIColor.blue.cgColor itemAddField.layer.shadowOffset = CGSize(width: 0, height: 0) itemAddField.layer.shadowOpacity = 0.1 // for tableview self.tableView.layer.cornerRadius = 8 self.tableView.clipsToBounds = true // for shadow UIView shadowView.layer.cornerRadius = 8.0 shadowView.layer.shadowColor = UIColor.blue.cgColor shadowView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0) shadowView.layer.shadowOpacity = 0.1 shadowView.layer.shadowRadius = 8.0 self.tableView.separatorInset.left = 0 // not scroll //self.tableView.isScrollEnabled = false self.tableView.dataSource = self self.tableView.delegate = self self.tableView.reloadData() view.layoutIfNeeded() view.updateConstraints() self.navigationController?.setNavigationBarHidden(true, animated: true) self.navigationController?.navigationBar.isHidden = true self.navigationController?.isNavigationBarHidden = true } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) tableHeight.constant = CGFloat(tableView.contentSize.height) } }

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

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

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

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

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

TsukubaDepot

2020/10/05 04:27

この現象が確認できる程度の最低限のサンプルコードは挙げていただくことは可能でしょうか。
sunglass

2020/10/05 04:56

コードを添付しました。。 UITableViewにshadow追加しているのですがその辺の処理がセルのpaddingを発生させているのでしょうか。
guest

回答1

0

ベストアンサー

どのような意図で以下の値をセットされているかわかりませんが、次の行をコメントアウトすれば、とりあえずヘッダの左余白はセルの左余白と同じになるかと思います。

Swift

1 self.tableView.separatorInset.left = 0

投稿2020/10/05 05:28

TsukubaDepot

総合スコア5086

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

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

sunglass

2020/10/05 05:58 編集

// self.tableView.separatorInset.left = 0 self.tableView.separatorInset.right = 16 で余白を「調整する」方向で実装しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問