首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在表格中插入单元格时,UITableViewCell不使用自动布局高度

在表格中插入单元格时,UITableViewCell不使用自动布局高度
EN

Stack Overflow用户
提问于 2018-12-05 04:04:33
回答 2查看 345关注 0票数 2

背景

我使用purelayout以编程方式创建我的UITableViewCells,遵循here的说明,基本上说明您必须在单元格上设置顶部/底部约束,然后使用

代码语言:javascript
复制
self.tableView.rowHeight = UITableViewAutomaticDimension;

要使其正确,请执行以下操作:

问题

除了在tableView中插入新行之外,一切都很正常。我得到了这样的效果:https://youtu.be/eTGWsxwDAdk

解释:只要我单击一个小费单元格,表格就会插入一个驱动程序小费行。但是,您会注意到,当我刷新该部分(通过单击提示框)时,所有单元格的高度都莫名其妙地增加了,但当我再次单击提示框时,它们又回到了正常高度这是用下面的代码完成的

代码语言:javascript
复制
self.tableView.beginUpdates()
self.tableView.reloadSections(IndexSet(integer:1), with: .automatic)
self.tableView.endUpdates()

这是cellfor row的实现。

代码语言:javascript
复制
// init table
self.tableView.register(OrderChargeTableViewCell.self,
                            forCellReuseIdentifier: OrderChargeTableViewCell.regularCellIdentifier)
self.tableView.register(OrderChargeTableViewCell.self,
                            forCellReuseIdentifier: OrderChargeTableViewCell.boldCellIdentifier)

self.tableView.estimatedRowHeight = 25
self.tableView.rowHeight = UITableViewAutomaticDimension

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell: OrderChargeTableViewCell?
    if (indexPath.row == filteredModel.count-1) {
        cell = tableView.dequeueReusableCell(withIdentifier: OrderChargeTableViewCell.boldCellIdentifier,
                                             for: indexPath) as? OrderChargeTableViewCell
    } else if (indexPath.row < filteredModel.count) {
        cell = tableView.dequeueReusableCell(withIdentifier: OrderChargeTableViewCell.regularCellIdentifier,
                                             for: indexPath) as? OrderChargeTableViewCell
    }

    // add data to cell labels
    return cell!
}

这是UITableViewCell本身的代码:

最终类OrderChargeTableViewCell: UITableViewCell {

代码语言:javascript
复制
// MARK: - init
static let boldCellIdentifier = "TTOrderDetailBoldTableViewCell"
static let regularCellIdentifier = "TTOrderDetailRegularTableViewCell"

private var didSetupConstraints = false
.. 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    self.keyLabel = TTRLabel()
    self.valueLabel = TTRLabel()

    if (reuseIdentifier == OrderChargeTableViewCell.regularCellIdentifier) {
        self.isCellStyleBold = false
    } else if (reuseIdentifier == OrderChargeTableViewCell.boldCellIdentifier) {
        self.isCellStyleBold = true
    } else {
        self.isCellStyleBold = false
        assertionFailure( "Attempt to create an OrderCharge cell with the wrong cell identifier: \(String(describing: reuseIdentifier))")
    }

    super.init(style: style, reuseIdentifier: reuseIdentifier)


    contentView.addSubview(keyLabel)
    contentView.addSubview(valueLabel)

}

override func updateConstraints()
{
    if !didSetupConstraints {
        if (isCellStyleBold) {
            self.applyBoldFormatting()
        } else {
            self.applyRegularFormatting()
        }

        didSetupConstraints = true
    }

    super.updateConstraints()
}
public func applyBoldFormatting() {
    keyLabel.font = .ttrSubTitle
    valueLabel.font = .ttrBody

    keyLabel.autoPinEdge(.leading, to: .leading, of: contentView, withOffset: 15)
    keyLabel.autoAlignAxis(.vertical, toSameAxisOf: contentView)

    keyLabel.autoPinEdge(.top, to: .top, of: contentView, withOffset: 8)
    keyLabel.autoPinEdge(.bottom, to: .bottom, of: contentView, withOffset: -8)

    valueLabel.autoPinEdge(.trailing, to: .trailing, of: contentView, withOffset: -15)
    valueLabel.autoAlignAxis(.baseline, toSameAxisOf: keyLabel)
}

public func applyRegularFormatting() {
    keyLabel.font = .ttrCaptions
    valueLabel.font = TTRFont.Style.standard(.h3).value

    keyLabel.autoPinEdge(.leading, to: .leading, of: contentView, withOffset: 15)
    keyLabel.autoAlignAxis(.vertical, toSameAxisOf: contentView)

    keyLabel.autoPinEdge(.top, to: .top, of: contentView, withOffset: 6)
    keyLabel.autoPinEdge(.bottom, to: .bottom, of: contentView, withOffset: -4)

    valueLabel.autoPinEdge(.trailing, to: .trailing, of: contentView, withOffset: -15)
    valueLabel.autoAlignAxis(.baseline, toSameAxisOf: keyLabel)
}

插入的驱动程序提示行具有单元格的标准44像素高度:

而其他(格式正确的)单元格的高度为25:

EN

回答 2

Stack Overflow用户

发布于 2018-12-05 04:26:59

这是一种暴力解决方案,我一点也不以此为荣,但这里仅供参考(不会将其标记为正确答案):

代码语言:javascript
复制
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let orderChargesSection = self.getOrderChargesSection()
    switch indexPath.section {
        case orderChargesSection:
            return self.getCellHeightForOrderCharges(row: indexPath.row)
        default:
            return UITableViewAutomaticDimension
    }
}

private func getCellHeightForOrderCharges(row: Int) -> CGFloat {
   let numRows = self.tableView(self.tableView, numberOfRowsInSection: self.getOrderChargesSection())
    if (row == numRows - 1) {
        return UITableViewAutomaticDimension
    } else {
        return 25.5
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-10-11 05:33:59

在执行beginUpdates / endUpdate和所有插入操作后,执行以下操作:

代码语言:javascript
复制
DispatchQueue.main.async {
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53620610

复制
相关文章

相似问题

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