Ios sizeToFit不在编程tableview单元格上工作

Ios sizeToFit不在编程tableview单元格上工作,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,我有一个基本的自定义单元格,左侧有一个名称标签,右侧有一个价格标签,都设置在另一个视图中,用于自定义间距。我希望价格将宽度更改为任何价格,而不是设置它,但是当我在cells init或cellForRow的函数中对价格使用sizetofit时,什么都不会发生。我环顾四周,但不知道如何使它工作。我无法在单元格的init中获取文本大小,但在cellForRowAt中设置标签大小似乎不正确 func tableView(_ tableView: UITableView, cellForRowAt in

我有一个基本的自定义单元格,左侧有一个名称标签,右侧有一个价格标签,都设置在另一个视图中,用于自定义间距。我希望价格将宽度更改为任何价格,而不是设置它,但是当我在cells init或cellForRow的函数中对价格使用sizetofit时,什么都不会发生。我环顾四周,但不知道如何使它工作。我无法在单元格的init中获取文本大小,但在cellForRowAt中设置标签大小似乎不正确

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

    let cell: SplitterCarouselItemTableViewCell = tableView.dequeueReusableCell(withIdentifier: "splitterCarouselItemTableViewCell") as! SplitterCarouselItemTableViewCell
    var item = ((allBillSplitters[tableView.tag].items)?.allObjects as! [Item])[indexPath.row]
    if allBillSplitters[tableView.tag].isMainBillSplitter {
        getMainBillSplitterItems(splitter: allBillSplitters[tableView.tag])
        item = mainBillSplitterItems[indexPath.row]
    }
    let count = item.billSplitters?.count

    if count! > 1 {
        cell.name!.text = "\(item.name!)\nsplit \(count!) ways"
        cell.price!.text = "£\(Double(item.price)/Double(count!))"

    } else {
        cell.name!.text = item.name!
        cell.price!.text = "£\(item.price)"
    }

    return cell
}
这是我的手机:

import UIKit

class SplitterCarouselItemTableViewCell: UITableViewCell {

    var name: UILabel!
    var price: UILabel!
    var view: UIView!

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:)")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: "splitterCarouselItemTableViewCell")

        self.setupViews()
    }

    func setupViews() {

        let width = Int(UIScreen.main.bounds.width * 0.88)
        let height = Int(self.bounds.height)

        self.backgroundColor = .clear
        self.frame = CGRect(x: 0, y: 0, width: width, height: 45)

        view = UIView(frame: CGRect(x: 0, y: 2, width: width, height: height - 4 ))
        view.backgroundColor = UIColor.white.withAlphaComponent(0.5)

        price = UILabel(frame: CGRect(x: width - 80, y: 0, width: 75, height: height))
        price.font = UIFont.systemFont(ofSize: 15.0)
        price.backgroundColor = .yellow
        price.textAlignment = .right

        name = UILabel(frame: CGRect(x: 5, y: 0, width: Int(price.frame.width), height: height))
        name.backgroundColor = .red
        name.font = UIFont.systemFont(ofSize: 15.0)
        name.numberOfLines = 0

        view.addSubview(name)
        view.addSubview(price)

        contentView.addSubview(view)
    }
}
任何帮助都会很好,我肯定我错过了一些基本的东西

我在屏幕截图中添加了黄色和红色背景以提高可视性


您需要根据文本计算价格标签的宽度。下面的代码将帮助您找到宽度

extension String {
    func widthWithConstrainedHeight(height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height )
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)

        return boundingBox.width
    }
}

使用上述函数查找字符串宽度,并使用该宽度创建价格标签框架。

我不清楚您是要调整框架大小还是字体大小。这个答案假设后者,也就是说,你想要一个固定的框架宽度和一个根据需要缩小的字体来适应

尝试启用adjustsFontSizeToFitWidth


这将缩小字体以适应标签的宽度。

您可以添加结果的图像吗?为什么不使用约束并在故事板或Nib中创建自定义单元格?@JasmeetKaur单元格和tableview是编程的,因为它们嵌套在旋转木马/幻灯片视图中。我只是需要价格标签来适应里面的价格。我只是尝试在cellForRowAt中使用它,但没有任何变化。我想我用错了,虽然这看起来很凌乱,但目前它是这样的。price.frame=CGRectx:cell.price.frame.width-80,y:0,width:cell.price.text?.widthWithConstrainedHeightheight:cell.view.frame.height,font:UIFont.systemFontofSize:15!,高度:cell.view.frame.height-4是否可以打印值cell.price.text?.widthWithConstrainedHeightheight:cell.view.frame.height,font:UIFont.systemFontofSize:15
price = UILabel(frame: CGRect(x: width - 80, y: 0, width: 75, height: height))
price.font = UIFont.systemFont(ofSize: 15.0)
price.backgroundColor = .yellow
price.textAlignment = .right
price.adjustsFontSizeToFitWidth = true