【问题标题】:HeightForRowAt indexPath issueHeightForRowAt indexPath 问题
【发布时间】:2018-07-16 18:36:39
【问题描述】:

我正在使用电子商务应用程序,但在尝试设置特定行的高度时遇到问题。当我选择类别时,表格会重新加载,并且根据我选择的类别,或多或少的行会通过 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

【问题讨论】:

  • 当您选择其他类别时,您是否致电reloadData()
  • 是的。 everytime ,还有一个设置额外字段的函数
  • tableCells 中的值放在哪里?这是您的实际代码吗?您的单元格出队代码将崩溃,因为它不允许在第一次使用该形式的出队函数使单元格出队时返回 nil
  • 我有,但是有很多代码我不想在这里全部链接

标签: ios swift uitableview swift3


【解决方案1】:

在设置单元格时尝试使用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 { ... }

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多