tableviewでsection数が固定であれば、
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if section == 0 { return 5 }else{ return 30 } }
このように記載出来ますが、section数がapiのデータにより動的に変わる場合
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if section == 0 { return 10 }else if section == 1 { return 5 }else if section == 2 { return 5 }else if section == 3 { return 5 }else if section == 4 { return 5 }else if section == 5{ return 5 }else{ return 5 } }
else if section==1〜section == 5以降までの部分をループなどで対応したいのですが、
エラーなどで出来ません。どのように解決すれば良いでしょうか??
具体的には
func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 0 { guard let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? Headcell else { fatalError("Your cellIdentifier is invalid") } cell.data(head_img[0]) return cell }else if indexPath.section == 1{ guard let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? Headcell else { fatalError("Your cellIdentifier is invalid") } guard self.banner.count != 0 else { return cell } if self.status[0] == 1000 { cell.data(self.state[0]) }else if self.status[0] == 1001{ cell.data(self.state2[0]) } return cell }else if indexPath.section == 2{ guard let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? Headcell else { fatalError("Your cellIdentifier is invalid") } guard self.banner.count != 0 else { return cell } if self.status[1] == 1000 { cell.data(self.state[1]) }else if self.status[1] == 1001{ cell.data(self.state2[1]) } return cell }else{ guard let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? Headcell else { fatalError("Your cellIdentifier is invalid") } guard self.banner.count != 0 else { return cell } if self.status[2] == 1000 { cell.data(self.state[2]) }else if self.status[2] == 1001{ cell.data(self.state2[2]) } return cell } } class Headcell: UITableViewCell { @IBOutlet weak var head: UIImageView! func data(_ img:UIImage) { head.image = img } }
エラーが出来るコードとエラー内容を書いて下さい。
すみません。returnの内容、本文を少し変更しました。私はsection ==1 〜secrtion ==5以降はfor in でまとめられないかと考えましたが、else if section == 1{の前にfor inを入れることが出来ずに質問に至りました。
現在の下のコードを見ると、section=0のときは高さ10、それ以外のときは高さ5になっていますが、これで正しいのでしょうか?
そうです。修正させて頂きました。お手数おかけして申し訳ありません。この場合ですとsection ==1 〜secrtion ==5以降はループでまとめられるのでしょうか?
return section == 0 ? 10 : 5 でダメな理由を教えて下さい。質問内容から仕様を読み取ることが出来ません。
return section == 0 ? 10 : 5このように省略して書くことができるのですか?これはsectionが0の時に何を表しているのでしょうか?(泣)
if section == 0 { return 10 } else { return 5 } と同じ意味です。
勉強になります、ありがとうございます。この場合ですと、return section == i ? 10 : 5でiを1ずつ増えていくようにループするとelseまで回数分増えませんか?
仕様を書く気はないのでしょうか?
本文内容を修正いたしました。
都合上全文を記載することは出来ませんが、該当部分は本文を修正して追加した部分です。section==0ではhead画像を取得しており、それ以外のsecgtionで同じような処理が行われます。この部分をループで記載出来ないかと思い質問に至った次第です。
section0じゃなくてsection1なんですか?
すみません、修正しました。section==0の間違いでした
結局のところ、「section=0のときは高さ10、それ以外のときは高さ5」ではないのでしょうか?
本文中2番目に表示しております、例文では値がreturn5で同じですが、具体的なコードは配列の添字の数がそれぞれのsectionごとに1増えていますそこは対応できなくないですか??(泣)
意味分からんw
本文中の具体的にはから以下のコードでsection==0はheaderの情報、それ以外のsectionの内容はほぼ同じですが、
if self.status[0] == 1000 {
cell.data(self.state[0])
}else if self.status[0] == 1001{
cell.data(self.state2[0])
}
このself.status[0]の添字を表してる数字だけ増えていってますよね?
そこは「section=0のときは高さ10、それ以外のときは高さ5」この考え方では出来なくないですか?
そのself.status[0]の添字部分が増えるようにループ処理を書いてあげることはしなくていいのか聞きたかったのです。