回答編集履歴

6

修正

2016/06/28 22:46

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -1,18 +1,4 @@
1
- 純正の`UITableViewCell`を使用している場合は以下の様に`heightForRowAtIndexPath`の中でラベルの高さを計算してセル自体の高さを返す様に実装しないと高さは変更されません。
2
-
3
- ※ とりあえず以下のメソッドで`100`とか返す複数行は表示されるはずです。`cell.textLabel.sizeToFit() `は無くてもOK
1
+ 以下のサイト参考に通常のCellカスタムセル例を作ってみました、これでCellの高が自動で計算されると思います。
4
-
5
-
6
-
7
- ```swift
8
-
9
- func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
10
-
11
- return 44 // ラベルの高さを計算してセル自体の高さを返す
12
-
13
- }
14
-
15
- ```
16
2
 
17
3
 
18
4
 

5

修正

2016/06/28 22:46

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -14,8 +14,6 @@
14
14
 
15
15
  ```
16
16
 
17
- 一番簡単なやり方は`CustomCell`を使用して`Autolayout`を適用する方法だと思います。
18
-
19
17
 
20
18
 
21
19
  [UITableViewCellの高さを自動で計算する: UITableViewAutomaticDimension](http://tomoyaonishi.hatenablog.jp/entry/2014/09/27/161152)
@@ -24,9 +22,81 @@
24
22
 
25
23
 
26
24
 
27
- 以下に確認した最低限のコードを載せておきます。
25
+ ## デフォルトのUITableViewCellを使用した方法
28
26
 
27
+
28
+
29
+ ```swift
30
+
31
+
32
+
33
+ import UIKit
34
+
35
+
36
+
37
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
38
+
39
+
40
+
41
+ @IBOutlet weak var myTableView: UITableView!
42
+
43
+
44
+
45
+ override func viewDidLoad() {
46
+
47
+ super.viewDidLoad()
48
+
49
+
50
+
51
+ myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
52
+
53
+
54
+
55
+ myTableView.estimatedRowHeight = 20
56
+
29
- この場合k`heightForRowAtIndexPath`メソッドは必要ありません。
57
+ myTableView.rowHeight = UITableViewAutomaticDimension
58
+
59
+ }
60
+
61
+
62
+
63
+ // Data Array
64
+
65
+ var dataArray = ["複数行文字1、複数行文字1、複数行文字1、複数行文字1、複数行文字1、複数行文字1、複数行文字1","複数行文字2、複数行文字2、複数行文字2、複数行文字2","文字3","文字4","文字5","文字6","文字7"]
66
+
67
+
68
+
69
+ // Row Count
70
+
71
+ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
72
+
73
+ return dataArray.count
74
+
75
+ }
76
+
77
+
78
+
79
+ // Generate Cell
80
+
81
+ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
82
+
83
+ let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
84
+
85
+ cell.textLabel?.numberOfLines = 0
86
+
87
+ cell.textLabel?.text = dataArray[indexPath.row]
88
+
89
+ return cell
90
+
91
+ }
92
+
93
+ }
94
+
95
+ ```
96
+
97
+
98
+
99
+ ## カスタムセルを使用した方法
30
100
 
31
101
 
32
102
 

4

修正

2016/06/28 22:43

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
 
20
20
 
21
- [UITableViewCellの高さを自動調整してくれるUITableViewAutomaticDimension](http://llcc.hatenablog.com/entry/2015/12/02/234751)
21
+ [UITableViewCellの高さを自動で計算す: UITableViewAutomaticDimension](http://tomoyaonishi.hatenablog.jp/entry/2014/09/27/161152)
22
22
 
23
23
  ※参考URL
24
24
 

3

修正

2016/06/28 22:34

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -1,4 +1,6 @@
1
1
  純正の`UITableViewCell`を使用している場合は以下の様に`heightForRowAtIndexPath`の中でラベルの高さを計算してセル自体の高さを返す様に実装しないと高さは変更されません。
2
+
3
+ ※ とりあえず以下のメソッドで`100`とかを返すと複数行では表示されるはずです。`cell.textLabel.sizeToFit() `は無くてもOK
2
4
 
3
5
 
4
6
 
@@ -23,6 +25,8 @@
23
25
 
24
26
 
25
27
  以下に確認した最低限のコードを載せておきます。
28
+
29
+ ※ この場合k`heightForRowAtIndexPath`メソッドは必要ありません。
26
30
 
27
31
 
28
32
 

2

修正

2016/06/28 22:31

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
36
36
 
37
-
37
+ // Storyboardに乗せたUITableView
38
38
 
39
39
  @IBOutlet weak var myTableView: UITableView!
40
40
 
@@ -90,6 +90,8 @@
90
90
 
91
91
  class CustomCell: UITableViewCell {
92
92
 
93
+ // StoryboardのCustomCellにラベルを一つ乗せ紐つける
94
+
93
95
  @IBOutlet weak var label: UILabel!
94
96
 
95
97
  }

1

修正

2016/06/28 22:23

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ```
14
14
 
15
- 一番簡単なやり方は`CustomCell`を用して`Autolayout`を使用するやり方だと思います。
15
+ 一番簡単なやり方は`CustomCell`を使用して`Autolayout`を用する方だと思います。
16
16
 
17
17
 
18
18
 
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- 以下に最低限のコードを載せておきます。
25
+ 以下に確認した最低限のコードを載せておきます。
26
26
 
27
27
 
28
28