UITableViewCell中被格式化为默认大小

问题描述 投票:0回答:1

我前几次已设定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)
    }
}     
ios swift uitableview
1个回答
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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.