二つのtableViewCellのそれぞれの高さを設定したい
swiftでApple music APIを使ったコードを書いています
発生している問題・エラーメッセージ
一方のセルの高さを40に、もう一方を140に設定したいのですが、両方40になって、一方のセルが途中で切れている
該当のソースコード
swift
1 var canMusicCatalogPlayback = false 2 override func viewDidLoad() { 3 super.viewDidLoad() 4 tableView.delegate = self 5 tableView.dataSource = self 6 7 prepare() 8 } 9 func prepare() { 10 guard albumID == nil else{ 11 apiClient.album(id: albumID!) { [unowned self] album in 12 DispatchQueue.main.async { 13 self.album = album 14 self.tableView.reloadData() 15 } 16 } 17 return 18 } 19 20 self.cloudServiceController.requestCapabilities { capabilities, error in 21 guard capabilities.contains(.musicCatalogPlayback) else { return } 22 self.canMusicCatalogPlayback = true 23 } 24 } 25} 26 27extension AlbumViewController:UITableViewDataSource,UITableViewDelegate{ 28 func numberOfSections(in tableView: UITableView) -> Int { 29 guard album != nil else { return 0 } 30 return 2 31 } 32 33 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 34 guard let album = album else { return 0 } 35 if section == 0 { 36 return 1 37 } else { 38 return album.relationships!.tracks!.count 39 } 40 } 41 42 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 43 if indexPath.section == 0 { 44 let cell = tableView.dequeueReusableCell(withIdentifier: "AlbumHeaderCell", for: indexPath) as! AlbumHeaderCell 45 cell.nameLabel.text = album!.attributes?.name 46 cell.artistLabel.text = album!.attributes?.artistName 47 cell.yearlabel.text = album!.attributes?.releaseDate 48 if let url = album!.attributes?.artwork?.imageURL(width: 220, height: 220) { 49 apiClient.image(url: url) { image in 50 cell.thumbnailView.image = image 51 } 52 } 53 return cell 54 } 55 56 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 57 let track = album?.relationships!.tracks![indexPath.row] 58 cell.textLabel?.text = track?.attributes?.name 59 return cell 60 } 61 62 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 63 64 tableView.estimatedRowHeight = 40 //セルの高さ 65 return UITableView.automaticDimension //自動設定 66 } 67 68 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 69 tableView.deselectRow(at: indexPath, animated: true) 70 guard canMusicCatalogPlayback else { return } 71 guard indexPath.section == 1 else { return } 72 73 } 74}
試したこと
storyboardでそれぞれのセルの高さを調整したり、コードをいろいろ試してみましたが、一向にできません。。。。。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/02 01:41 編集