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

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

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

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

Q&A

解決済

1回答

1649閲覧

【Swift】UITableViewの表示がおかしいが原因がわからない。

Yutaka.engin

総合スコア2

Swift

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

0グッド

0クリップ

投稿2021/10/12 03:14

前提・実現したいこと

タブ管理画面を作成中
UITableViewをセクションで分けて表示したい。

発生している問題

  • sectionの内容が繰り返されている?
  • 下にスクロールして戻ると編集モードがsection0にも適用されてしまっている

どこが原因かわからず悩んでおります、お力を貸していただきたいです????‍♂️

イメージ説明

該当のソースコード

Swift

1import Foundation 2import UIKit 3 4class TabTitleController: BaseViewController, UITableViewDelegate, UITableViewDataSource { 5 6 // AppDelegate 7 fileprivate var delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate 8 9 fileprivate var items = Memo().loadTab() 10 fileprivate let colors = Theme().load() 11 12 fileprivate let addTextField = UITextField() 13 fileprivate let textField = UITextField() 14 fileprivate let addButton = UIButton() 15 // displayの幅と高さ 16 fileprivate let dw: CGFloat = UIScreen.main.bounds.size.width 17 fileprivate let dh: CGFloat = UIScreen.main.bounds.size.height 18 19 fileprivate var tableView: UITableView? 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 // ナビゲーションバーの設定 24 doNavigationBarSetting() 25 addButton.addTarget(self, action: #selector(self.addButtonTapped(_:)), for: .touchUpInside) 26 27 28 // TableViewの生成する 29 tableView = UITableView(frame: CGRect(x: 0, y: 0, width: dw, height: dh), style: .grouped) 30 31 32 // Cell名の登録をおこなう. 33 tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "tabTitleCell") 34 tableView!.delegate = self 35 tableView!.dataSource = self 36 tableView!.tableFooterView = UIView(frame: .zero) 37 view.addSubview(tableView!) 38 39 } 40 41 func numberOfSections(in tableView: UITableView) -> Int { 42 return 2 43 } 44 45 46 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 47 if section == 0{ 48 return "タブ追加" 49 }else{ 50 return "タブ名変更・削除・並び替え" 51 } 52 } 53 54 /* 55 * Cellの総数を返すデータソースメソッド(実装必須) 56 */ 57 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 58 if section == 0 { 59 return 2 60 }else{ 61 return items.count 62 } 63 } 64 65 /* 66 * Cellのデザインなどを定義 67 */ 68 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 69 let cell = tableView.dequeueReusableCell(withIdentifier: "tabTitleCell", for: indexPath) 70 let row = indexPath.row 71 cell.selectionStyle = .none 72 if indexPath.section == 0 { 73 if indexPath.row == 0 { 74 addTextField.frame = CGRect(x: 15, y: 0, width: self.dw * 0.6, height: 45) 75 addTextField.placeholder = "タブ名を入力してください" 76 cell.contentView.addSubview(addTextField) 77 }else{ 78 addButton.frame = CGRect(x: self.dw / 2 - self.dw * 0.3, y: 90 / 2 - 20, width: self.dw * 0.6, height: 40) 79 addButton.backgroundColor = .black 80 addButton.setTitle("追加", for: .normal) 81 addButton.setTitleColor(.white, for: .normal) 82 addButton.layer.borderColor = UIColor.gray.cgColor 83 addButton.layer.borderWidth = 0.5 84 addButton.layer.cornerRadius = 5 85 cell.backgroundColor = .clear 86 cell.contentView.addSubview(addButton) 87 } 88 89 }else{ 90 tableView.setEditing(true, animated: true) 91 // タブタイトル編集用のTextField 92 textField.frame = CGRect(x: 15, y: 0, width: dw * 0.6, height: 45) 93 textField.text = items[row] 94 cell.contentView.addSubview(textField) 95 96 } 97 return cell 98 99 } 100 101 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 102 if indexPath.section == 0 && indexPath.row == 1 { 103 return 90 104 } else { 105 return 45 106 } 107 }

補足情報(FW/ツールのバージョンなど)

Xcode 13.0
Swift 5

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

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

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

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

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

guest

回答1

0

ベストアンサー

色々と質問外のところで思うところはありますが、今回は触れません。

#sectionの内容が繰り返されている?
UItableViewのCellは実は内部的には使い回しされているので起こってしまう現象になります。
簡単な解決策としてSectionごとでCellを分けてあげれば良いかと。

Section1とSection2のCellをそれぞれTableViewに登録する

Swift

1 tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Section1TabTitleCell") 2 tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Section2TabTitleCell")

cellForRowAtでSectionごとにcellを設定する。

Swift

1 if indexPath.section == 0 { 2 cell = tableView.dequeueReusableCell(withIdentifier: "Section1TabTitleCell", for: indexPath) 3 4 }else{ 5 cell = tableView.dequeueReusableCell(withIdentifier: "Section2TabTitleCell", for: indexPath) 6 }

しっかりと対応したい場合はCustomCellを作った方良いです。

#下にスクロールして戻ると編集モードがsection0にも適用されてしまっている
CellごとにEditingの設定をするだけです。

Swift

1Section1は編集なし、Section2は編集ありの場合 2 3 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 4 if indexPath.section == 0 { 5 return false 6 } else { 7 return true 8 } 9 }

もちろんindexPath.rowを使えばCell単位で制御も可能です。

投稿2021/10/12 05:47

shiokara

総合スコア95

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問