我正在使用一款电子商务应用程序,在尝试设置特定行的高度时遇到了一个问题。当我选择类别时,表会重新加载,根据我选择的类别,通过api的行数或多或少。
因此,在此设置中,当我选择一个特定类别时,它可以正常工作,但当我选择另一个类别时,它将使我的行变乱,并将高度增加到另一个类别。
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呢?
发布于 2018-02-12 23:05:47
在设置单元格时尝试使用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 { ... }https://stackoverflow.com/questions/48647410
复制相似问题