前提・実現したいこと
以前の質問で作成したUITableViewに以下の3つの機能を実装したいです。(※コード全容は記事下部にあります)
・UITableView内のテキストを折り返しにしたい。
・UITableViewの高さをテキストの多さによって可変
・UITableView内のImageViewの幅を固定してAspectFitにしたい
(※折り返しにして文字の高さによってUITableViewが可変になるが画像サイズはそのまま。というイメージ)
発生している問題・エラーメッセージ
######問題点①TableView内のテキストを折り返ししようとすると、UIが崩れる
文字を折り返しをするために
こちらのサイトを参考に以下のコードを追加したところ
swift
1cell.textLabel?.numberOfLines=0
画像がどっかーんってなりました。
でも文字はちゃんと縦になっているので、
改行はされていると思われます。
######問題点②TableView内のimageViewが制御できない。
問題点①はおそらく
「UITableView内のImageViewの高さと幅が固定されていないからだな」
と思ったので、こちらのサイトを参考に以下のコードを「cellForRowAt indexPath」に追加
@IBOutlet weak var image: UIImageView! --省略-- image.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
すると以下のエラーコード
error
1The image outlet from the ViewController to the UIImageView is invalid. Outlets cannot be connected to repeating content. [12] 2 3和約:ViewControllerからUIImageViewへの画像アウトレットが無効です。 コンセントを繰り返しコンテンツに接続することはできません。 [12]
繰り返し…?
TableViewを繋げているから、
その中のImgeviewを繋げようとして怒られるのでしょうか…?
TableView内のImageViewをOutlet接続以外で制御する方法を知りたいです。
######問題点③セルの可変
こちらのサイトを参考に以下のコードを追加したいのですが、問題点①と②の影響で、正しいのかどうかを確認できず。
一旦これをなくしたら画像が大きくならないのかを確認してみようと思います。
swift
1commentTableView.estimatedRowHeight = 66 2commentTableView.rowHeight = UITableViewAutomaticDimension
該当のソースコード
swift
1import UIKit 2 3struct HogeItem: Decodable { 4 let Name: String 5 let Image: String 6} 7 8struct HogeSection: Decodable { 9 let SectionName: String 10 let Items: [HogeItem] 11} 12 13var _items: [HogeSection] = [] 14 15class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 16 // MARK: - pattern②Plistあり 17 @IBOutlet weak var commentTableView: UITableView! 18 @IBOutlet weak var image: UIImageView! 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 23 if let path = Bundle.main.path(forResource: "DisneyCharacterList", ofType:"plist" ) { 24 let data = try! Data(contentsOf: URL(fileURLWithPath: path)) 25 _items = try! PropertyListDecoder().decode([HogeSection].self, from: data) 26 } 27 28 //高さを可変にする 29 commentTableView.estimatedRowHeight = 66 30 commentTableView.rowHeight = UITableView.automaticDimension 31 } 32 33 // 設定 34 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 35 return _items.count 36 } 37 38 // 設定(行数) 39 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 40 return _items[section].Items.count 41 } 42 43 //セクション数の設定 44 func numberOfSections(in tableView: UITableView) -> Int { 45 return _items.count 46 } 47 48 // 設定(セクションタイトル) 49 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 50 return _items[section].SectionName 51 } 52 53 // 設定(セル) 54 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 55 let item = _items[indexPath.section].Items[indexPath.row] 56 57 let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath) 58 cell.textLabel?.numberOfLines=0 59 cell.textLabel?.text = item.Name 60 cell.imageView?.image = UIImage(named: item.Image) 61 62 //セルのサイズを指定 63 image.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 64 return cell 65 } 66 67 //セルタップ時にセルの内容を取得する 68 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 69 let item = _items[indexPath.section].Items[indexPath.row] 70 71 print(item.Name) 72 } 73} 74 75
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/09 12:41
2020/09/09 13:09 編集
2020/09/10 07:48 編集