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

質問編集履歴

9

試した事を追記しました。

2021/12/10 16:57

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -44,33 +44,26 @@
44
44
  }
45
45
 
46
46
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
47
- let cell = TableView.dequeueReusableCell(withIdentifier: "TweetCell", for: indexPath)
47
+ let cell = TableView.dequeueReusableCell(withIdentifier: "TweetCell", for: indexPath) as! TweetCell
48
-
48
+
49
49
  func getTweet () -> Void{
50
50
 
51
51
  swifter.getTimeline(for: .screenName("@xxxxxxxx"),success: { json in
52
52
 
53
- tableView.rowHeight = UITableView.automaticDimension
54
- cell.textLabel?.numberOfLines = 0
55
- cell.textLabel?.sizeToFit()
56
- cell.textLabel?.lineBreakMode = .byWordWrapping
57
- cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
53
+ cell.tweetLabel?.text = "(json[indexPath.row]["text"]),(json[indexPath.row]["text"]),(json[indexPath.row]["text"]),(json[indexPath.row]["text"])"
58
54
 
59
- cell.textLabel?.text = json[indexPath.row]["text"].string
60
-
61
55
  }, failure: { error in
62
56
  // 失敗時の処理
63
57
  print(error)
64
58
  print("失敗")
65
59
  })
66
-
67
60
  }
68
-
69
61
  _ = getTweet()
70
62
 
71
63
  return cell
72
64
  }
73
65
  }
66
+
74
67
  ```
75
68
 
76
69
  ### 試したこと
@@ -79,6 +72,11 @@
79
72
 
80
73
  「tableViewCell 高さ」とか「dequeueReusableCell リセット」とか検索して、見つかった限りのやり方は一通り試したのですがうまく行きませんでした。
81
74
 
75
+ TableViewControllerから作ってみたり、UIViewControllerにTableViewやTableViewCellを配置して作ってみたりしたのですが上手くいきませんでした。
76
+
77
+ また、TableViewCellにlabelを置く時にコードで書いて配置してみたり、ストーリーボードでドラッグ&ドロップで配置してみたりしたのですがうまく行きませんでした。
78
+
79
+
82
80
  ### 補足情報(FW/ツールのバージョンなど)
83
81
 
84
82
  Xcode11.6

8

試した事を加筆しました。

2021/12/10 16:57

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,8 @@
8
8
  ツイートを並べる事はできたのですが、ツイートの内容が長いと途中までしか表示されません。
9
9
  また、内容が短くても不要な余白が生じてしまいます。
10
10
 
11
+ 最初はどのセルも2行で途中までしか表示されません。
11
- セルの再利用の際のリセットがうまく行っていないのか、スクロールするとセルの高さがおかしくなります。
12
+ また、セルの再利用の際のリセットがうまく行っていないのか、スクロールするとセルの高さがおかしくなります。
12
13
 
13
14
  セルの再利用の際にセルの高さをリセットできれば上手く表示できそうなのですが、リセットの仕方が分かりません。
14
15
  セルの高さのリセットの方法を教えていただけると助かります。
@@ -76,6 +77,8 @@
76
77
 
77
78
  ラベルに制約を付けたり、estimateRowHeightとautomaticDimentionを記述してみたりもしたのですが上手く行きませんでした。Twitterからのデータでなく、適当な配列を用意して試すとestimatedRowHeightとautomaticDimentionだけでも上手く行くのですが、Twitterからのデータを取得して並べようとすると上手く行きません。
78
79
 
80
+ 「tableViewCell 高さ」とか「dequeueReusableCell リセット」とか検索して、見つかった限りのやり方は一通り試したのですがうまく行きませんでした。
81
+
79
82
  ### 補足情報(FW/ツールのバージョンなど)
80
83
 
81
84
  Xcode11.6

7

試した事を少し修正しました。

2021/12/10 12:22

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -74,7 +74,7 @@
74
74
 
75
75
  ### 試したこと
76
76
 
77
- ラベルに制約を付けたり、estimateRowHeightとautomaticDimentionを記述してみたりもしたのですが上手く行きませんでした。
77
+ ラベルに制約を付けたり、estimateRowHeightとautomaticDimentionを記述してみたりもしたのですが上手く行きませんでした。Twitterからのデータでなく、適当な配列を用意して試すとestimatedRowHeightとautomaticDimentionだけでも上手く行くのですが、Twitterからのデータを取得して並べようとすると上手く行きません。
78
78
 
79
79
  ### 補足情報(FW/ツールのバージョンなど)
80
80
 

6

さらに自分でアレコレ試した事を踏まえて質問内容を書き換えました。

2021/12/07 15:52

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
@@ -1,1 +1,1 @@
1
- 制約うまく付けられません。スクロールするとエラーが出ます。
1
+ SwiftのTableCell再利用する際にセルの高さをリセットしたい
body CHANGED
@@ -1,25 +1,22 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- Swifterを使って自作のアプリにTwitterの自分のツイートを20件並べたい。
3
+ Swifterを使って自作のアプリにTwitterの自分のツイートを内容を省略せずに20件並べたい。
4
4
 
5
5
  ここに質問の内容を詳しく書いてください。
6
6
 
7
7
  Swifterを使って自分のツイートを並べるだけのアプリを作っています。
8
8
  ツイートを並べる事はできたのですが、ツイートの内容が長いと途中までしか表示されません。
9
+ また、内容が短くても不要な余白が生じてしまいます。
9
10
 
10
- セルの再利用の際のリセットがうまく行っていないというもあるも知れないとは思うのですが自分でも分かるのは制約うま付けられていいという事です。制約はコードで書いて付けたいと思っています。
11
+ セルの再利用の際のリセットがうまく行っていないのか、スクロールすとセル高さおかしくなます。
11
12
 
12
- まずは制約正し付けたいと思っています。
13
+ セルの再利用の際にセルの高さリセットできれば上手表示できそうなのでが、リセットの仕方が分かりません
13
- 正しい制約付け方、あるいは今制約どこが足りないのかを探る方法をいただけると助かります。
14
+ セル高さリセットの方法を教えていただけると助かります。
14
15
 
15
16
  ### 発生している問題・エラーメッセージ
16
17
 
17
- ビルド時にはエラー出ていないのですが、画面を下にスクロールすると以下のようなエラーが出
18
+ エラー出ていません
18
19
 
19
- ```
20
- 2021-12-07 03:57:37.526755+0900 SwifterTwitterTimeline[15108:9108237] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead. Cell: <UITableViewCell: 0x100b39da0; frame = (0 826; 375 59); text = 'iOSアプリ開発の勉強中です。再度テストツイート。'; clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x283c8ae80>>
21
- ```
22
-
23
20
  ### 該当のソースコード
24
21
 
25
22
  ```Swift
@@ -51,18 +48,13 @@
51
48
  func getTweet () -> Void{
52
49
 
53
50
  swifter.getTimeline(for: .screenName("@xxxxxxxx"),success: { json in
54
-
51
+
55
52
  tableView.rowHeight = UITableView.automaticDimension
56
53
  cell.textLabel?.numberOfLines = 0
57
54
  cell.textLabel?.sizeToFit()
58
55
  cell.textLabel?.lineBreakMode = .byWordWrapping
59
- cell.textLabel!.translatesAutoresizingMaskIntoConstraints = false
56
+ cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
60
57
 
61
- cell.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
62
- cell.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor).isActive = true
63
- cell.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor).isActive = true
64
- cell.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
65
-
66
58
  cell.textLabel?.text = json[indexPath.row]["text"].string
67
59
 
68
60
  }, failure: { error in
@@ -82,11 +74,8 @@
82
74
 
83
75
  ### 試したこと
84
76
 
85
- ラベルの高さについて制約を付けても見たのですが上手く行きませんでした。
86
- の再利用の際リセットできていない事が悪いのかも知れないのでestimateRowHeightとautomaticDimentionを記述してみたりもしたのですが上手く行きませんでした。
77
+ ラベルに制約を付けたり、estimateRowHeightとautomaticDimentionを記述してみたりもしたのですが上手く行きませんでした。
87
78
 
88
- 制約が正しく付けられていないのか、セルの再利用の際にリセットできていないのか(リセットの仕方もわかりません。高さの制約を0で付けてみたりしたのですが・・・)、その両方なのかも判断がつきません。
89
-
90
79
  ### 補足情報(FW/ツールのバージョンなど)
91
80
 
92
81
  Xcode11.6

5

質問内容に少し加筆しました。

2021/12/06 21:36

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
  Swifterを使って自分のツイートを並べるだけのアプリを作っています。
8
8
  ツイートを並べる事はできたのですが、ツイートの内容が長いと途中までしか表示されません。
9
9
 
10
- セルの再利用の際のリセットがうまく行っていないというのもあると思うのですが、それ以前に制約がうまく付けられていない状態です。
10
+ セルの再利用の際のリセットがうまく行っていないというのもあるかも知れない思うのですが、自分でも分かるのは制約がうまく付けられていないという事です。制約はコードで書いて付けたいと思っています。
11
11
 
12
12
  まずは制約を正しく付けたいと思っています。
13
13
  正しい制約の付け方、あるいは今の制約のどこが足りないのかを探る方法をご教示いただけると助かります。

4

回答が付かない間に自分でアレコレやったので、それを踏まえて質問内容を変えました。

2021/12/06 19:19

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
@@ -1,1 +1,1 @@
1
- 自作アプリに自分のツイート並べたいのですがautomaticDimentionがうまく働いてくれません。
1
+ 制約をうまく付けられません。スクロールするとエラーが出ます。
body CHANGED
@@ -5,22 +5,19 @@
5
5
  ここに質問の内容を詳しく書いてください。
6
6
 
7
7
  Swifterを使って自分のツイートを並べるだけのアプリを作っています。
8
- ツイートを並べる事はできたのですが、heightForRowAtのautomaticDimentionでセルの高さを自動計算させ、ツイートの内容を全部表示させたいのです、内容が1行だけのセルでは高さが余計に算出され、複数行に渡る内容の場合は勝手にツイートの途中から途切てしう状況です
8
+ ツイートを並べる事はできたのですが、ツイートの内容が長いと途中までし表示されません
9
9
 
10
- 毎回特定のセル(ツイート)表示がおかしいというよりは、スクロールさせる度に新たに表示されるセルの高さがおかしくなる状態で、何というか、もしかしたらセルの再利用の際に高さがリセットされていないのれません
10
+ セルの再利用の際リセットがうまく行っていないというのもあると思うのですが、そ以前に制約がうく付けられていない状態です
11
11
 
12
- セルの高さを正しく計算されるようにしたいと思っています。
12
+ まずは制約を正しく付けたいと思っています。
13
+ 正しい制約の付け方、あるいは今の制約のどこが足りないのかを探る方法をご教示いただけると助かります。
13
14
 
14
15
  ### 発生している問題・エラーメッセージ
15
16
 
16
- 現時点ではエラーは特に出ていませんが、
17
- 制約つけた場合estimateRowHeightやautomaticDimentionと併用すると以下のような
17
+ ビルド時にはエラーが出ていないのですが、画面スクロールすると以下のようなエラーが出ます。
18
- エラーが出ます。
19
18
 
20
- 実現したいことができればestimateRowHeightやautomaticDimentionを使うでも制約を使うでも良いのですが、どちらも使わない、または併用するではうまく行っていません。
21
-
22
19
  ```
23
- 2021-12-06 00:38:46.078434+0900 SwifterTwitterTimeline[14651:8903291] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x281a9f4c0 "UITableViewLabel:0x10fe30770.top"> and <NSLayoutYAxisAnchor:0x281a9e2c0 "UITableViewCellContentView:0x10fe2fa40.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.
20
+ 2021-12-07 03:57:37.526755+0900 SwifterTwitterTimeline[15108:9108237] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead. Cell: <UITableViewCell: 0x100b39da0; frame = (0 826; 375 59); text = 'iOSアプリ開発の勉強中です。再度テストツイート。'; clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x283c8ae80>>
24
21
  ```
25
22
 
26
23
  ### 該当のソースコード
@@ -30,46 +27,44 @@
30
27
  import UIKit
31
28
  import Swifter
32
29
 
33
- class TimelineViewController: UITableViewController {
30
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
34
31
 
35
-
36
32
  var tweet: [JSON] = []
37
33
 
38
- let swifter = Swifter(consumerKey: "xxxxxxxxxxxxxxxxxxxxxx", consumerSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
34
+ let swifter = Swifter(consumerKey: "xxxxxxxxxxxxxxxxxxxxxxxxx", consumerSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
39
35
 
36
+ @IBOutlet weak var TableView: UITableView!
37
+
40
38
  override func viewDidLoad() {
41
39
  super.viewDidLoad()
40
+ TableView.dataSource = self
41
+ TableView.delegate = self
42
42
  }
43
43
 
44
- // MARK: - Table view data source
45
-
46
- override func numberOfSections(in tableView: UITableView) -> Int {
44
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
47
- // #warning Incomplete implementation, return the number of sections
48
- return 1
45
+ return 20
49
46
  }
50
47
 
51
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
48
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
49
+ let cell = TableView.dequeueReusableCell(withIdentifier: "TweetCell", for: indexPath)
52
50
 
53
- return 20
54
- }
55
-
56
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
57
- let cell = tableView.dequeueReusableCell(withIdentifier: "TweetCell", for: indexPath)
58
-
59
51
  func getTweet () -> Void{
60
52
 
61
53
  swifter.getTimeline(for: .screenName("@xxxxxxxx"),success: { json in
62
-
54
+
63
- cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
55
+ tableView.rowHeight = UITableView.automaticDimension
64
-
65
56
  cell.textLabel?.numberOfLines = 0
66
-
67
57
  cell.textLabel?.sizeToFit()
58
+ cell.textLabel?.lineBreakMode = .byWordWrapping
59
+ cell.textLabel!.translatesAutoresizingMaskIntoConstraints = false
68
60
 
61
+ cell.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
62
+ cell.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor).isActive = true
63
+ cell.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor).isActive = true
64
+ cell.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
65
+
69
- cell.textLabel?.lineBreakMode = .byWordWrapping
66
+ cell.textLabel?.text = json[indexPath.row]["text"].string
70
67
 
71
- cell.textLabel?.text = json[indexPath.row]["text"].string
72
-
73
68
  }, failure: { error in
74
69
  // 失敗時の処理
75
70
  print(error)
@@ -82,44 +77,16 @@
82
77
 
83
78
  return cell
84
79
  }
85
-
86
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
87
- tableView.estimatedRowHeight = 50
88
- tableView.rowHeight = UITableView.automaticDimension
89
- return tableView.rowHeight
90
- }
91
80
  }
92
81
  ```
93
82
 
94
83
  ### 試したこと
95
84
 
85
+ ラベルの高さについて制約を付けても見たのですが上手く行きませんでした。
96
- estimateRowHeightとautomaticDimentionをcellForRowAtに書んでり、
86
+ セルの再利用の際にリセットできていない事が悪いのかも知れないのでestimateRowHeightとautomaticDimentionを記述してみたりもしたのですが上手く行ませんで
97
- tableViewCellのラベルの上下左右に制約を付けてみたり、その両方をしてみたりしたのですが
98
- 上手く行きませんでした。
99
87
 
100
- TwitterAPIのデータを使わずに配列を用意してセルに代入するだけならestimateRowHeightとautomaticDimentionだけでセの高さは正く自動計算されましたが、TwitterAPIからデータを取得した途端にセル高さ自動計算がおしくなり
88
+ 制約が正く付けられいないのか、セルの再利用の際リセットきていないのか(リット仕方もわかりません。高さの制約を0で付けてみたりしたのです・・・)両方なのかも判断がつきせん
101
89
 
102
- 制約を付ける場合は以下のような感じで付けていました。
103
-
104
- ```Swift
105
-
106
- cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
107
-
108
- cell.textLabel?.numberOfLines = 0
109
-
110
- cell.textLabel?.sizeToFit()
111
-
112
- cell.textLabel?.lineBreakMode = .byWordWrapping
113
-
114
- cell.textLabel?.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
115
- cell.textLabel?.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor).isActive = true
116
- cell.textLabel?.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor).isActive = true
117
- cell.textLabel?.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
118
-
119
- cell.textLabel?.text = json[indexPath.row]["text"].string
120
- ```
121
-
122
-
123
90
  ### 補足情報(FW/ツールのバージョンなど)
124
91
 
125
92
  Xcode11.6

3

試した事を少し修正しました。

2021/12/06 19:14

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -97,7 +97,7 @@
97
97
  tableViewCellのラベルの上下左右に制約を付けてみたり、その両方をしてみたりしたのですが
98
98
  上手く行きませんでした。
99
99
 
100
- TwitterAPIのデータを使わずに配列を用意して各セルに代入するだけならestimateRowHeightとautomaticDimentionも制約も付ない状態でセルの高さは正しく自動計算されましたが、TwitterAPIからデータを取得した途端にセルの高さの自動計算がおかしくなります。
100
+ TwitterAPIのデータを使わずに配列を用意して各セルに代入するだけならestimateRowHeightとautomaticDimentionけでセルの高さは正しく自動計算されましたが、TwitterAPIからデータを取得した途端にセルの高さの自動計算がおかしくなります。
101
101
 
102
102
  制約を付ける場合は以下のような感じで付けていました。
103
103
 

2

試したことにさらに試した事を追記しました。

2021/12/05 18:27

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -97,6 +97,8 @@
97
97
  tableViewCellのラベルの上下左右に制約を付けてみたり、その両方をしてみたりしたのですが
98
98
  上手く行きませんでした。
99
99
 
100
+ TwitterAPIのデータを使わずに配列を用意して各セルに代入するだけならestimateRowHeightとautomaticDimentionも制約も付けない状態でセルの高さは正しく自動計算されましたが、TwitterAPIからデータを取得した途端にセルの高さの自動計算がおかしくなります。
101
+
100
102
  制約を付ける場合は以下のような感じで付けていました。
101
103
 
102
104
  ```Swift

1

質問の内容をより詳しく書き直しました。

2021/12/05 17:59

投稿

tmyk1979
tmyk1979

スコア145

title CHANGED
File without changes
body CHANGED
@@ -7,6 +7,8 @@
7
7
  Swifterを使って自分のツイートを並べるだけのアプリを作っています。
8
8
  ツイートを並べる事はできたのですが、heightForRowAtのautomaticDimentionでセルの高さを自動計算させ、ツイートの内容を全部表示させたいのですが、内容が1行だけのセルでは高さが余計に算出され、複数行に渡る内容の場合は勝手にツイートの途中から途切れてしまう状況です。
9
9
 
10
+ 毎回特定のセル(ツイート)の表示がおかしいというよりは、スクロールさせる度に新たに表示されるセルの高さがおかしくなる状態で、何というか、もしかしたらセルの再利用の際に高さがリセットされていないのかも知れません。
11
+
10
12
  セルの高さを正しく計算されるようにしたいと思っています。
11
13
 
12
14
  ### 発生している問題・エラーメッセージ