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

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

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

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

Q&A

解決済

1回答

1441閲覧

tableViewの編集モードで選択不可にしたセルのインデント?を選択可のセルと同じにしたい。

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2020/03/19 15:31

編集2020/03/19 15:44

いつもお世話になります。

tableViewisEditingとallowsMultipleSelectionDuringEditingtrueにしてから一部セルを選択不可にしているのですが、
選択不可にしたセルのcontentViewの幅が縮小せずラベルの先頭にインデントがついたものとインデントがついていないものが混在してしまいます。
出来合いのプロパティ/メソッド等で回避する方法はありますか?

イメージ説明

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 let array = [String](repeating: "A", count: 5) 6 7 @IBOutlet weak var containerView: UIView! 8 @IBOutlet weak var upperView: UIView! 9 @IBOutlet weak var tTableView: UITableView! 10 11 @IBOutlet weak var upperViewBottom: NSLayoutConstraint! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 tTableView.isEditing = true 17 tTableView.allowsMultipleSelectionDuringEditing = true 18 } 19 20} 21 22extension ViewController: UITableViewDataSource { 23 24 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 25 return array.count 26 } 27 28 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 29 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 30 31 cell.textLabel?.text = array[indexPath.row] 32 33 cell.selectionStyle = indexPath.row.isEvenNumber ? .default : .none 34 35 return cell 36 } 37 38 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 39 40 return indexPath.row.isEvenNumber 41 42 } 43} 44 45extension ViewController: UITableViewDelegate { 46 47 // willSelectRow 48 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 49 return indexPath.row.isEvenNumber ? indexPath : nil 50 } 51 52 // indicateEditButton 53 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { 54 return UITableViewCell.EditingStyle.init(rawValue: 3)! 55 } 56 57 // spaceOfEditIco 58 func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPaht: IndexPath) -> Bool { 59 return true 60 } 61 62 func scrollViewDidScroll(_ scrollView: UIScrollView) { 63 64 if scrollView.contentOffset.y < 0 { 65 66 let v: CGFloat = min(abs(scrollView.contentOffset.y), upperView.frame.height) 67 scrollView.contentInset.top = v 68 upperViewBottom.constant = -v 69 70 } else { 71 scrollView.contentInset.top = 0 72 upperViewBottom.constant = 0 73 } 74 } 75} 76 77extension Int { 78 79 var isEvenNumber: Bool { 80 return self % 2 == 0 81 } 82} 83

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

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

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

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

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

guest

回答1

0

ベストアンサー

cellForRowAt内で↓とりあえずこれで。isEditingを動的に変化させたときはどうすれば?

swift

1cell.indentationLevel = indexPath.row.isEvenNumber ? 0 : 4 2 3cell.indentationWidth = indexPath.row.isEvenNumber ? 0 : 10

イメージ説明

投稿2020/03/19 16:27

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問