HeightForRowAt indexPath 问题
HeightForRowAt indexPath issue
我正在开发一个电子商务应用程序,但在尝试设置特定行的高度时遇到问题。当我选择类别时,table 会重新加载,并且根据我选择的类别,或多或少的行会通过 api。
因此,使用此设置,当我 select 一个特定类别时它可以工作,但是当我选择另一个类别时,它会弄乱我的行并将高度增加到另一个
let firstCellHeight:CGFloat = 265
let addBtnCellHeight:CGFloat = 60
let phoneCellHeight: CGFloat = 95
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cellIdentifier = self.tableCells[indexPath.row]
let height:CGFloat!
switch cellIdentifier {
case "AddAdImagesCell":
height = self.firstCellHeight
case "AddBtnCell":
height = self.addBtnCellHeight
case "ExtraFieldCell":
if indexPath.row == 4 {
height = self.phoneCellHeight
} else {
height = 44
}
default:
height = 44
}
return height
}
对于具有以下代码的第 i 行的单元格:
case "ExtraFieldCell":
let currentField = self.extraFields[indexPath.row - self.firstExtraFieldIndex]
switch currentField.type {
case "select":
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldSelectCell")!
if currentField.name == "area"
return cell
case "text":
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldTextCell") as! FiltersTFCell
cell.label.text = "\(currentField.label):"
return cell
case "checkbox":
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldCheckboxCell") as! CheckboxCell
cell.fieldLabel.text = currentField.label
return cell
default:
let cell = UITableViewCell()
return cell
}
那么如何为具有 case "text"
的行设置高度始终为 95
尝试在设置单元格时使用self.tableView.rowHeight
,我尝试使用下面的示例代码,如果您有任何问题请给我回电。
case "ExtraFieldCell":
let currentField = self.extraFields[indexPath.row - self.firstExtraFieldIndex]
switch currentField.type {
case "select":
self.tableView.rowHeight = firstCellHeight
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldSelectCell")!
if currentField.name == "area"
return cell
case "text":
self.tableView.rowHeight = addBtnCellHeight
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldTextCell") as! FiltersTFCell
cell.label.text = "\(currentField.label):"
return cell
case "checkbox":
self.tableView.rowHeight = phoneCellHeight
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldCheckboxCell") as! CheckboxCell
cell.fieldLabel.text = currentField.label
return cell
default:
self.tableView.rowHeight = 44
let cell = UITableViewCell()
return cell
}
重要的是,什么时候测试注释或者去掉这个方法
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { ... }
我正在开发一个电子商务应用程序,但在尝试设置特定行的高度时遇到问题。当我选择类别时,table 会重新加载,并且根据我选择的类别,或多或少的行会通过 api。
因此,使用此设置,当我 select 一个特定类别时它可以工作,但是当我选择另一个类别时,它会弄乱我的行并将高度增加到另一个
let firstCellHeight:CGFloat = 265
let addBtnCellHeight:CGFloat = 60
let phoneCellHeight: CGFloat = 95
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cellIdentifier = self.tableCells[indexPath.row]
let height:CGFloat!
switch cellIdentifier {
case "AddAdImagesCell":
height = self.firstCellHeight
case "AddBtnCell":
height = self.addBtnCellHeight
case "ExtraFieldCell":
if indexPath.row == 4 {
height = self.phoneCellHeight
} else {
height = 44
}
default:
height = 44
}
return height
}
对于具有以下代码的第 i 行的单元格:
case "ExtraFieldCell":
let currentField = self.extraFields[indexPath.row - self.firstExtraFieldIndex]
switch currentField.type {
case "select":
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldSelectCell")!
if currentField.name == "area"
return cell
case "text":
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldTextCell") as! FiltersTFCell
cell.label.text = "\(currentField.label):"
return cell
case "checkbox":
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldCheckboxCell") as! CheckboxCell
cell.fieldLabel.text = currentField.label
return cell
default:
let cell = UITableViewCell()
return cell
}
那么如何为具有 case "text"
尝试在设置单元格时使用self.tableView.rowHeight
,我尝试使用下面的示例代码,如果您有任何问题请给我回电。
case "ExtraFieldCell":
let currentField = self.extraFields[indexPath.row - self.firstExtraFieldIndex]
switch currentField.type {
case "select":
self.tableView.rowHeight = firstCellHeight
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldSelectCell")!
if currentField.name == "area"
return cell
case "text":
self.tableView.rowHeight = addBtnCellHeight
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldTextCell") as! FiltersTFCell
cell.label.text = "\(currentField.label):"
return cell
case "checkbox":
self.tableView.rowHeight = phoneCellHeight
let cell = tableView.dequeueReusableCell(withIdentifier: "ExtraFieldCheckboxCell") as! CheckboxCell
cell.fieldLabel.text = currentField.label
return cell
default:
self.tableView.rowHeight = 44
let cell = UITableViewCell()
return cell
}
重要的是,什么时候测试注释或者去掉这个方法
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { ... }