UITableViewCell被格式化为默认大小

时间:2019-02-03 00:30:19

标签: ios swift uitableview

我已经对UITableViews进行了几次编程。我真的看不到我在做什么。我正在向视图添加UILabels,但我认为它们的添加是相对于默认UITableView单元格大小的大小。当我省略heightForRowAt时,视图的顶部和底部文本大约在单元格的顶部和底部,因此我认为heightForRowAt函数正在发生某些事情,并且高度没有传递给我的子类。另外,我将文本对齐方式设置为在右侧标签的右边,这也不起作用。

Here is what my table looks like

When I don't implement heightForRowAt

我还将在这里发布重要的类型代码。也许有一个我没有看到的问题。

override init(frame: CGRect) {
    super.init(frame:frame)
    let tblViewFrame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
    tableView = UITableView(frame: tblViewFrame, style: .plain)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(FeedViewCell.self, forCellReuseIdentifier: feedCellID)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: feedCellID, for: indexPath)
    }
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //not going to be this big, just for testing
        return CGFloat(100)
}

我也很确定我的CGRect数学是正确的,因为我以多种方式重做了它,但是它从来没有用过。对于UITableViewCell,我添加了一个UIView作为其内部包含所有文本的子视图。任何帮助将不胜感激

import Foundation
import UIKit

class FeedViewCell:UITableViewCell {

    var titleLabel:UILabel!
    var subtitleLabel:UILabel!
    var topRightLabel:UILabel!
    var bottomRightLabel:UILabel!
    var innerView:UIView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style:style, reuseIdentifier:reuseIdentifier)
        formatCell()
    }

    func formatCell() {
        backgroundColor = colorSet.sideViewBackgroundColor
        createLabels()
        formatLabels()
        print(frame.height)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func formatLabels() {
        //titleLabel
        titleLabel.text = "Top Left"
        titleLabel.font = fontSet.feedViewTitleFont

        //subtitleLabel
        subtitleLabel.text = "Bottom Left"

        //topRightLabel
        topRightLabel.text = "Top Right"
        topRightLabel.textAlignment = .right

        //bottomRightLabel
        bottomRightLabel.text = "Bot Right"
        bottomRightLabel.textAlignment = .right
    }

    func createLabels() {

        //Space so stuff doesn't touch edges
        let bufferSpace = CGFloat(0)

        let horDelim = self.frame.width * CGFloat(0.75)
        let verDelim = self.frame.height * CGFloat(0.5)

        let tlRect = CGRect(x: bufferSpace, y: bufferSpace, width: horDelim - bufferSpace, height: verDelim - bufferSpace)
        let trRect = CGRect(x: horDelim, y: bufferSpace, width: self.frame.width - horDelim - bufferSpace, height: self.frame.height - verDelim - bufferSpace)
        let blRect = CGRect(x: bufferSpace, y: verDelim, width: horDelim - bufferSpace, height: self.frame.height - verDelim - bufferSpace)
        let brRect = CGRect(x: horDelim, y: verDelim, width: self.frame.width - horDelim - bufferSpace, height: self.frame.height - verDelim - bufferSpace)

        innerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))

        titleLabel = UILabel(frame: tlRect)
        subtitleLabel = UILabel(frame: blRect)
        topRightLabel = UILabel(frame: trRect)
        bottomRightLabel = UILabel(frame: brRect)

        innerView.addSubview(titleLabel)
        innerView.addSubview(subtitleLabel)
        innerView.addSubview(topRightLabel)
        innerView.addSubview(bottomRightLabel)
        addSubview(innerView)
    }
}     

1 个答案:

答案 0 :(得分:0)

下面是你会如何手动摆出细胞的例子。对于每个内容视图,您需要确定内容的高度和宽度,并计算它们相对于单元格中其他项目的对齐方式。这是一个简单的示例,因为标签不是多行的,我只是随意地使每个内容视图的宽度为单元格宽度的一半。

您还需要在heightForRowAtIndexPath中设置单元格的大小。在过去的自动布局之前,收集意见的工作,我用了一个尺寸小区(小区的静态实例),在那里我可以通过在细胞中的内容,然后获得基于它会显示内容的单元格高度。

import UIKit

class FeedViewCell:UITableViewCell {

    var titleLabel:UILabel!
    var subtitleLabel:UILabel!
    var topRightLabel:UILabel!
    var bottomRightLabel:UILabel!
    var innerView:UIView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style:style, reuseIdentifier:reuseIdentifier)
        formatCell()
    }

    func formatCell() {
        createLabels()
        formatLabels()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func formatLabels() {
        //titleLabel
        titleLabel.text = "Top Left"
        titleLabel.font = UIFont.systemFont(ofSize: 20.0)
        titleLabel.backgroundColor = .cyan

        //subtitleLabel
        subtitleLabel.text = "Bottom Left"
        subtitleLabel.font = UIFont.systemFont(ofSize: 15.0)
        subtitleLabel.backgroundColor = .orange

        //topRightLabel
        topRightLabel.text = "Top Right"
        topRightLabel.font = UIFont.systemFont(ofSize: 20.0)
        topRightLabel.textAlignment = .right
        topRightLabel.backgroundColor = .yellow

        //bottomRightLabel
        bottomRightLabel.text = "Bottom Right"
        bottomRightLabel.font = UIFont.systemFont(ofSize: 15.0)
        bottomRightLabel.textAlignment = .right
        bottomRightLabel.backgroundColor = .green
    }

    func createLabels() {
        //Space so stuff doesn't touch edges
        titleLabel = UILabel(frame: .zero)
        subtitleLabel = UILabel(frame: .zero)
        topRightLabel = UILabel(frame: .zero)
        bottomRightLabel = UILabel(frame: .zero)

        contentView.addSubview(titleLabel)
        contentView.addSubview(subtitleLabel)
        contentView.addSubview(topRightLabel)
        contentView.addSubview(bottomRightLabel)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        /* title label */
        let marginSpacing:CGFloat = 5
        let itemSpacing:CGFloat = 5

        var width = frame.width * 0.5
        var height = calculateTextHeight(width: width, text: titleLabel.text!, attributes: [NSAttributedString.Key.font: titleLabel.font!])
        let titleRect = CGRect(x: marginSpacing, y: 8, width: width, height: height)

        /* subtitle label */
        width = titleRect.width
        height = calculateTextHeight(width: width, text: subtitleLabel.text!, attributes: [NSAttributedString.Key.font: subtitleLabel.font!])
        let subtitleRect = CGRect(x: marginSpacing, y: titleRect.maxY + itemSpacing, width: width , height: height)

        /* top right label */
        width = frame.width - titleRect.width - marginSpacing
        height = calculateTextHeight(width: width, text: topRightLabel.text!, attributes: [NSAttributedString.Key.font: topRightLabel.font!])
        let topRightRect = CGRect(x: titleRect.width, y: titleRect.origin.y, width: width, height: height)

        /* bottom right label */
         width = frame.width - subtitleRect.width - marginSpacing
        height = calculateTextHeight(width: width, text: bottomRightLabel.text!, attributes: [NSAttributedString.Key.font: bottomRightLabel.font!])
        let bottomRightRect = CGRect(x: subtitleRect.width, y: topRightRect.maxY + itemSpacing, width: width, height: height)

        titleLabel.frame =  titleRect
        subtitleLabel.frame = subtitleRect
        topRightLabel.frame = topRightRect
        bottomRightLabel.frame = bottomRightRect
        print("maxY", bottomRightLabel.frame.maxY)
    }

    func calculateTextHeight(width:CGFloat, text:String, attributes:[NSAttributedString.Key:UIFont]) -> CGFloat {
        let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
        let labelRect = text.boundingRect(with: size, options:[.usesLineFragmentOrigin, .usesFontLeading], attributes:attributes, context: nil)
        let height = ceil(labelRect.height)
        return height
    }

    override var intrinsicContentSize: CGSize {
        return CGSize(width: frame.width, height: frame.height)
    }
}