セルに画像とテキストを表示させようと、Xibファイルでカスタムセルを作りました。
画像とテキストがちゃんと表示されるように、RowHeight==300にしました。
しかし、セルの高さがそのままで、セルが崩れて表示されてしまいました。
念のため、heightForRowAt内に、
swift
1tableView.estimatedRowHeight = 300 2return UITableView.automaticDimension
を付け加えましたが、変わりませんでした。
シミュレータの機種を変更しても、やはりダメでした。
カスタムセルだと高さは変更できないのでしょうか?
もしくは、別の方法があるのでしょうか?
どうかご回答よろしくお願いします。
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier:"cell") } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell cell.setting(indexPath: indexPath) return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { tableView.estimatedRowHeight = 300 return UITableView.automaticDimension } }
import UIKit class TableViewCell: UITableViewCell { //曲名、アーティスト名、index、画像 var musiclibrary = [["songname":"ヒーリング1","artistname":"魔王魂","index":"0","image":"healing1.jpg"], ["songname":"ヒーリング2","artistname":"魔王魂","index":"1","image":"healing2.jpg"], ["songname":"ヒーリング3","artistname":"魔王魂","index":"2","image": "healing3.jpg"], ["songname":"バトル1","artistname":"魔王魂","index":"3","image":"battle.jpg"], ["songname":"サイバー","artistname":"魔王魂","index":"4","image": "cyber.jpg"]] @IBOutlet weak var imageview: UIImageView! @IBOutlet weak var songname: UILabel! @IBOutlet weak var artistname: UILabel! override func awakeFromNib() { super.awakeFromNib() } //ラベルと画像の内容 func setting(indexPath: IndexPath) { imageview.image = UIImage(named: musiclibrary[indexPath.row]["image"]!) songname.text = musiclibrary[indexPath.row]["songname"] artistname.text = musiclibrary[indexPath.row]["artistname"] } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
--TableViewCell.xib
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2018/09/21 15:50