swift:增加 TableViewCell 高度取决于 UILabel 而不使用约束

标签 swift uitableview uilabel height

我想增加 UITableviewCell 高度取决于 TableViewCell 按钮单击上的 UILabel 内容。 我对此进行了搜索,但我找到了有关限制的答案。

我想动态增加 UITabelViewCell 高度,而不使用“Constarints”/自动布局。

我使用以下代码获取标签高度:

func getLabelHeight(lbl : UILabel) -> CGFloat
    {
        let constraint = CGSizeMake(lbl.frame.size.width, CGFloat.max)
        var size = CGSize()

        let context = NSStringDrawingContext();

        let boundingBox : CGSize

        boundingBox = NSString(string: lbl.text!).boundingRectWithSize(constraint,
                                                                    options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                                                                    attributes: [NSFontAttributeName: self],
                                                                    context: context).size

        size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height))
        return size.height;
    }

但我无法在 UITableview 中使用此代码。为了增加 TableViewCell 高度,我编写了以下代码:

func btnReadMoreClick(sender: UIButton){
        let buttonTag = sender.tag

        indexOfReadMoreButton = sender.tag
        isReadMoreButtonTouched = true

        tableVDetail.beginUpdates()
        tableVDetail.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfReadMoreButton, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
        tableVDetail.endUpdates()
    }

并设置单元格高度

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

但是使用这个我无法得到我的输出。 :(

提前致谢!

最佳答案

如果您对单元设置了正确的约束,它应该完成其余的工作。

它是一个助手,可以自动调整单元格大小并计算预期高度。

extension UITableViewCell {
    func calcualteHeight(inTableview tableview: UITableView) -> CGFloat {
        self.setNeedsUpdateConstraints()
        self.updateConstraintsIfNeeded()
        self.bounds = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), CGRectGetHeight(self.bounds))
        self.setNeedsLayout()
        self.layoutIfNeeded()

        let height = self.contentView.systemLayoutSizeFittingSize(self.bounds.size, withHorizontalFittingPriority: UILayoutPriorityFittingSizeLevel, verticalFittingPriority: UILayoutPriorityFittingSizeLevel).height
        return height
    }
}

在 TableView 委托(delegate)中,获取计算单元格并填充数据。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       let cell = tableView.dequeueReusableCellWithIdentifier(self.viewModel.reuseIdentifier(atIndexPath: indexPath)) as! YourCell
       cell.setupData(fillCellYourData)
       return cell.calcualteHeight(inTableview: tableView)
}

关于swift:增加 TableViewCell 高度取决于 UILabel 而不使用约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38975268/

相关文章:

ios - Xcode存档后崩溃?

swift - ARKit 动画 SCNNode

ios - 如何自动将用户分为不同的组(身份池)?

ios - 自定义 uitableviewcell 标签隐藏/显示

ios - 按 NSDate 对表格部分进行排序

ios - 单元格中有不必要的填充的 UILabel

ios - 将 UITextfield "clear"默认按钮连接到 UILabel

swift - 如何使用 Swift 显示工具栏按钮并将其添加到 UITableViewController

ios - tableView Swift 中的乘积值

Xcode 期望堆栈 View 中的 UILabel 具有比实际高度更大的高度