【问题标题】:How do I have two separate cells with two different heights? [duplicate]如何拥有两个具有两个不同高度的独立单元? [复制]
【发布时间】:2017-04-19 17:25:06
【问题描述】:
我有两个自定义单元格,但是无法为这两个单元格设置静态高度,我需要第一个单元格的高度为 100,但其他每个单元格的高度为 40。下面的代码使所有单元格高度为 100 而不仅仅是第一个。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(indexPath.row == 0) {
return 100.0
}
return 40.0
}
【问题讨论】:
标签:
ios
swift
uitableview
【解决方案1】:
您可以将第一个单元格放在不同的部分。
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}else {
return yourArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! FirstCustomCell
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! SecondCustomCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 {
return 100
} else{
return 40
}