teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

訂正

2019/10/22 05:41

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- tableViewCell座標を変した値がios1213で違うことについて
1
+ tableViewcontentOffsetを変てスクロールさせ場合のiOS12iOS13の挙動のについて
body CHANGED
@@ -1,11 +1,19 @@
1
1
  `tableView`のセルをボタンタップで追加しているのですが、`iOS12`と`iOS13`で違う動作をしてしまいます。直す方法などご存知でしたら教えて下さい。
2
2
 
3
- cellを追加するスペースがない場合は`contentOffset`をいじって`tableView`をセルの高さ分スクロールさせてからセルを追加しい。
3
+ cellを追加するスペースがない場合は`contentOffset`をいじって`tableView`をセルの高さ分スクロールさせてからセルを追加します
4
+ 連続してセルを追加する場合、iOS12の場合は、contentOffset.yが意図したとおりに増加していきますが、iOS13の場合は1度しか増加せず、よって一度しか意図したとおりにスクロールしません。
4
5
 
6
+ contentOffsetをいじった場合の挙動が違うようですが、プログラムから変更させたcontentOffsetを定着?リフレッシュ??させるようなメソッドなどあるのでしょうか?
7
+
8
+ Xcode var 11.1
9
+ -simulator-
10
+ iOS 12.1 iPhone Xs Max
11
+ iOS 13.1 iPhone 11 Pro Max
12
+
5
13
  ↓iOS12.1:希望する動きになっている
6
- ![iOS12.1](d34f6455a2f4411a1e0f5eab9af94c0a.gif)
14
+ ![iOS12.1](81c6918d6d410d49b7cfe31c1e20afa6.gif)
7
15
  ↓iOS13.1:スクロールが変…
8
- ![イメージ説明](14fc023306c654030b073e8a94429207.gif)
16
+ ![iOS13.1](6982fd8df7603afc94ad70e64c70bc86.gif)
9
17
 
10
18
  ```swift
11
19
  import UIKit
@@ -14,7 +22,7 @@
14
22
 
15
23
  @IBOutlet weak var tTableView: UITableView!
16
24
 
17
- var array: [Int] = [1, 2, 3, 4]
25
+ var array: [Int] = [1, 2, 3, 4, 5, 6]
18
26
 
19
27
  override func viewDidLoad() {
20
28
  super.viewDidLoad()
@@ -32,33 +40,31 @@
32
40
  array.append(array.count + 1)
33
41
  // 追加したセルのインデックスパスを取得
34
42
  let addCellIndexPath = IndexPath(row: array.count - 1, section: 0)
35
- // 追加するセルの直上のセルのMaxYをself.view系に変換して取得 <= この値がおかしい??
43
+ // 追加するセルの直上のセルのMaxYをself.view系に変換して取得
36
44
  let lastCellMaxY
37
45
  = tTableView.convert(tTableView.rectForRow(at: lastCellIndexPath), to: self.view).maxY
38
46
 
39
-      // ログを下に貼っておきます
40
- print("### rectForRow.maxY:", tTableView.rectForRow(at: lastCellIndexPath).maxY)
41
- print("### lastCellMaxY:", lastCellMaxY)
42
-
43
47
  // tableViewのmaxYを取得
44
48
  let tTableViewMaxY = tTableView.frame.maxY
45
49
 
46
- // addCellを挿入するスペースがない場合の処理
50
+ // addCellを挿入するスペースがない場合の処理
47
- if lastCellMaxY + 40 >= tTableViewMaxY {
51
+ if lastCellMaxY + 40 > tTableViewMaxY {
48
-
52
+
49
53
  // スクロール量を求める
50
54
  let offsetY: CGFloat = 40 - (tTableViewMaxY - lastCellMaxY) + 0.5
51
55
 
56
+ // ログを下に貼っておきます
52
- //print("##############################################")
57
+ print("------------------------------------------------")
53
- //print("tTableViewMaxY:", tTableViewMaxY)
54
- //print("lastCellMaxY :", lastCellMaxY)
55
- //print("offsetY :", offsetY)
58
+ print("### offsetY:", offsetY)
59
+ print("### before tTableView.contentOffset.y:", tTableView.contentOffset.y)
56
60
 
57
- // スクロール完了後にセルを追加する
61
+ //// スクロール完了後にセルを追加する
58
62
  UIView.animate(withDuration: 0.6, animations: {
59
- self.tTableView.contentOffset.y += offsetY
63
+ self.tTableView.contentOffset.y += offsetY // <= 一度しか加算されない
60
64
  }){(finished) in
65
+ print("### after tTableView.contentOffset.y:", self.tTableView.contentOffset.y)
61
66
  self.tTableView.insertRows(at: [addCellIndexPath], with: .right)
67
+
62
68
  }
63
69
  // addCellを挿入するスペースがある場合の処理
64
70
  } else {
@@ -88,51 +94,72 @@
88
94
 
89
95
 
90
96
  /*
91
- ios12.1
97
+ ios12.1 contentOffsetYが増加していく スクロールさせる値のoffsetYは一定。
98
+ ------------------------------------------------
92
- ### rectForRow.maxY: 160.0
99
+ ### offsetY: 35.16666666666663
93
- ### lastCellMaxY: 351.0
94
- ### rectForRow.maxY: 200.0
100
+ ### before tTableView.contentOffset.y: 0.0
95
- ### lastCellMaxY: 391.0
96
- ### rectForRow.maxY: 240.0
101
+ ### after tTableView.contentOffset.y: 35.0
102
+ ------------------------------------------------
97
- ### lastCellMaxY: 431.0
103
+ ### offsetY: 40.5
104
+ ### before tTableView.contentOffset.y: 34.666666666666664
98
- ### rectForRow.maxY: 280.0
105
+ ### after tTableView.contentOffset.y: 75.0
106
+ ------------------------------------------------
99
- ### lastCellMaxY: 436.33333333333337
107
+ ### offsetY: 40.49999999999994
108
+ ### before tTableView.contentOffset.y: 74.66666666666667
100
- ### rectForRow.maxY: 320.0
109
+ ### after tTableView.contentOffset.y: 115.0
110
+ ------------------------------------------------
101
- ### lastCellMaxY: 436.3333333333333
111
+ ### offsetY: 40.49999999999994
112
+ ### before tTableView.contentOffset.y: 114.66666666666667
102
- ### rectForRow.maxY: 360.0
113
+ ### after tTableView.contentOffset.y: 155.0
103
- ### lastCellMaxY: 436.3333333333333
114
+ ------------------------------------------------
104
- ### rectForRow.maxY: 400.0
115
+ ### offsetY: 40.5
116
+ ### before tTableView.contentOffset.y: 154.66666666666666
105
- ### lastCellMaxY: 436.33333333333337
117
+ ### after tTableView.contentOffset.y: 195.0
118
+ ------------------------------------------------
106
- ### rectForRow.maxY: 440.0
119
+ ### offsetY: 40.5
120
+ ### before tTableView.contentOffset.y: 194.66666666666666
107
- ### lastCellMaxY: 436.33333333333337
121
+ ### after tTableView.contentOffset.y: 235.0
122
+ ------------------------------------------------
108
- ### rectForRow.maxY: 480.0
123
+ ### offsetY: 40.5
124
+ ### before tTableView.contentOffset.y: 234.66666666666666
125
+ ### after tTableView.contentOffset.y: 275.0
126
+ ------------------------------------------------
109
- ### lastCellMaxY: 436.33333333333337
127
+ ### offsetY: 40.49999999999994
110
- ### rectForRow.maxY: 520.0
128
+ ### before tTableView.contentOffset.y: 274.6666666666667
111
- ### lastCellMaxY: 436.3333333333333
129
+ ### after tTableView.contentOffset.y: 315.0
112
130
 
113
- iOS 13.1
114
131
 
132
+ iOS 13.1 contentOffset.yが一度しか増加せず、そのためスクロールさせる値のoffsetYが増加していく。
133
+ ------------------------------------------------
134
+ ### offsetY: 35.16666666666663
135
+ ### before tTableView.contentOffset.y: 0.0
136
+ ### after tTableView.contentOffset.y: 35.0
137
+ ------------------------------------------------
138
+ ### offsetY: 40.5
139
+ ### before tTableView.contentOffset.y: 34.666666666666664
140
+ ### after tTableView.contentOffset.y: 34.666666666666664
141
+ ------------------------------------------------
142
+ ### offsetY: 80.5
143
+ ### before tTableView.contentOffset.y: 34.666666666666664
144
+ ### after tTableView.contentOffset.y: 34.666666666666664
145
+ ------------------------------------------------
146
+ ### offsetY: 120.5
147
+ ### before tTableView.contentOffset.y: 34.666666666666664
148
+ ### after tTableView.contentOffset.y: 34.666666666666664
149
+ ------------------------------------------------
115
- ### rectForRow.maxY: 160.0
150
+ ### offsetY: 160.5
151
+ ### before tTableView.contentOffset.y: 34.666666666666664
116
- ### lastCellMaxY: 351.0
152
+ ### after tTableView.contentOffset.y: 34.666666666666664
153
+ ------------------------------------------------
117
- ### rectForRow.maxY: 200.0
154
+ ### offsetY: 200.5
155
+ ### before tTableView.contentOffset.y: 34.666666666666664
118
- ### lastCellMaxY: 391.0
156
+ ### after tTableView.contentOffset.y: 34.666666666666664
157
+ ------------------------------------------------
119
- ### rectForRow.maxY: 240.0
158
+ ### offsetY: 240.5
120
- ### lastCellMaxY: 431.0
121
- ### rectForRow.maxY: 280.0
122
- ### lastCellMaxY: 471.0
123
- ### rectForRow.maxY: 320.0
124
- ### lastCellMaxY: 476.33333333333337
159
+ ### before tTableView.contentOffset.y: 34.666666666666664
125
- ### rectForRow.maxY: 360.0
126
- ### lastCellMaxY: 516.3333333333334
160
+ ### after tTableView.contentOffset.y: 34.666666666666664
127
- ### rectForRow.maxY: 400.0
128
- ### lastCellMaxY: 556.3333333333334
129
- ### rectForRow.maxY: 440.0
130
- ### lastCellMaxY: 596.3333333333334
131
- ### rectForRow.maxY: 480.0
132
- ### lastCellMaxY: 636.3333333333334
133
- ### rectForRow.maxY: 520.0
134
- ### lastCellMaxY: 676.3333333333334
135
161
 
162
+
136
163
  */
137
164
 
138
165
 

1

ログ追加

2019/10/22 05:41

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -36,6 +36,7 @@
36
36
  let lastCellMaxY
37
37
  = tTableView.convert(tTableView.rectForRow(at: lastCellIndexPath), to: self.view).maxY
38
38
 
39
+      // ログを下に貼っておきます
39
40
  print("### rectForRow.maxY:", tTableView.rectForRow(at: lastCellIndexPath).maxY)
40
41
  print("### lastCellMaxY:", lastCellMaxY)
41
42
 
@@ -86,5 +87,53 @@
86
87
  }
87
88
 
88
89
 
90
+ /*
91
+ ios12.1
92
+ ### rectForRow.maxY: 160.0
93
+ ### lastCellMaxY: 351.0
94
+ ### rectForRow.maxY: 200.0
95
+ ### lastCellMaxY: 391.0
96
+ ### rectForRow.maxY: 240.0
97
+ ### lastCellMaxY: 431.0
98
+ ### rectForRow.maxY: 280.0
99
+ ### lastCellMaxY: 436.33333333333337
100
+ ### rectForRow.maxY: 320.0
101
+ ### lastCellMaxY: 436.3333333333333
102
+ ### rectForRow.maxY: 360.0
103
+ ### lastCellMaxY: 436.3333333333333
104
+ ### rectForRow.maxY: 400.0
105
+ ### lastCellMaxY: 436.33333333333337
106
+ ### rectForRow.maxY: 440.0
107
+ ### lastCellMaxY: 436.33333333333337
108
+ ### rectForRow.maxY: 480.0
109
+ ### lastCellMaxY: 436.33333333333337
110
+ ### rectForRow.maxY: 520.0
111
+ ### lastCellMaxY: 436.3333333333333
89
112
 
113
+ iOS 13.1
114
+
115
+ ### rectForRow.maxY: 160.0
116
+ ### lastCellMaxY: 351.0
117
+ ### rectForRow.maxY: 200.0
118
+ ### lastCellMaxY: 391.0
119
+ ### rectForRow.maxY: 240.0
120
+ ### lastCellMaxY: 431.0
121
+ ### rectForRow.maxY: 280.0
122
+ ### lastCellMaxY: 471.0
123
+ ### rectForRow.maxY: 320.0
124
+ ### lastCellMaxY: 476.33333333333337
125
+ ### rectForRow.maxY: 360.0
126
+ ### lastCellMaxY: 516.3333333333334
127
+ ### rectForRow.maxY: 400.0
128
+ ### lastCellMaxY: 556.3333333333334
129
+ ### rectForRow.maxY: 440.0
130
+ ### lastCellMaxY: 596.3333333333334
131
+ ### rectForRow.maxY: 480.0
132
+ ### lastCellMaxY: 636.3333333333334
133
+ ### rectForRow.maxY: 520.0
134
+ ### lastCellMaxY: 676.3333333333334
135
+
136
+ */
137
+
138
+
90
139
  ```