首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HeightForRowAt indexPath问题

HeightForRowAt indexPath问题
EN

Stack Overflow用户
提问于 2018-02-07 00:18:59
回答 1查看 267关注 0票数 0

我正在使用一款电子商务应用程序,在尝试设置特定行的高度时遇到了一个问题。当我选择类别时,表会重新加载,根据我选择的类别,通过api的行数或多或少。

因此,在此设置中,当我选择一个特定类别时,它可以正常工作,但当我选择另一个类别时,它将使我的行变乱,并将高度增加到另一个类别。

代码语言:javascript
复制
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行的单元格,代码如下:

代码语言:javascript
复制
 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呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-12 23:05:47

在设置单元格时尝试使用self.tableView.rowHeight,我尝试使用您下面的示例代码进行应用,如果您有任何问题,请联系我。

代码语言:javascript
复制
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
        }

重要的是,何时测试注释或删除此方法

代码语言:javascript
复制
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { ... }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48647410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档