如何为tableviewcell

时间:2018-01-31 17:51:16

标签: ios swift uitableview

我需要为UITableViewController下的UITableViewCell设置前导和尾随。

UITableViewCell的特定类,我在下面添加了代码。

 override func layoutSubviews() {
        self.frame = CGRect(x: 10.0, y:  self.frame.origin.y, width: 390.0, height: self.frame.size.height)
        super.layoutSubviews()
    }

以上在前导和尾随留下空格。但是当我在iPhone 5s和4模拟器中测试它导致水平滚动时的问题。

由于宽度不变,我教会了这个问题。 所以我从UITableViewcell获得了特定的标签字段宽度,并减去了10.0

我为宽度问题尝试了以下代码。但是,没有任何结果。

func setFrame(x: CGFloat, y: CGFloat , width: CGFloat , height: CGFloat ) {
        let newX = x.isNaN ? self.frame.origin.x : x
        let newY = y.isNaN ? self.frame.origin.y : y
        let newWidth = width.isNaN ? self.bounds.size.width : width
        let newHeight = height.isNaN ? self.bounds.size.height : height

        self.frame = CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
    }
    override func layoutSubviews() {
        let width = (greetingLabel.bounds.size.width - 10.0)
        setFrame(x: 5.0, y: 5.0, width:width, height: self.frame.height)
    }

我已经尝试将所有字段保留在View中,但UI看起来不太好。

请提供一些输入,了解如何设置UITableViewCell的前导和尾随。

我想实现这样的屏幕:

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

在你的情况下,我会让单元格的contentView透明(contentView.backgroundColor = .clear)。然后我会向单元格添加一个新属性,让我们将其称为newContentView,将其添加到contentView,然后根据需要使用约束来适应它。然后,只需使用newContentView作为您要添加内容的空间。

class CustomCell: UITableViewCell {
    fileprivate let newContentView = UIView()

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

        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear

        self.contentView.addSubview(newContentView)
        newContentView.backgroundColor = .white

        newContentView.translatesAutoresizingMaskIntoConstraints = false
        // in this case edge insets will be 10 per each side
        NSLayoutConstraint.activate([
            newContentView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10),
            newContentView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10),
            newContentView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -10),
            newContentView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10),
            ])

        // and from now on, use newContentView as the holder of the subviews
    }

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

答案 1 :(得分:1)

您不应该自己更改单元格的框架,因为它是Table / Collection视图的责任。如果看一下Cell,你可以注意到它总是有一个内容视图,所有元素都应放在它里面。不应该更改内容视图的约束或框架,但您可以将元素相对于它放置。

关于您的代码 - 设置框架与约束完全无关。 (当然你有 translatesAutoresizingMaskIntoConstraints == true 帧将由UIKit转换为约束,但它仍然不是设置约束

我建议您在界面构建器中进行布局,这样可以节省您的时间,使您的代码更清洁并提高您的技能。您可以将您的通道拖放到单元格中,选择它并设置所需的约束。

要从您的代码动态设置约束,您需要创建它们:

例如:

override func awakeFromNib() {
    super.awakeFromNib()
    /// Create your greeting label
    var  constraints = [NSLayoutConstraint]()
    constraints.append( greetingLabel.leftAnchor.constraintEqualToSystemSpacingAfter(contentView.leftAnchor, multiplier: 1.0))
    constraints.append( contentView.rightAnchor.constraintEqualToSystemSpacingAfter(greetingLabel.rightAnchor, multiplier: 1.0))
    constraints.append(contentView.centerYAnchor.constraint(equalTo: greetingLabel.centerYAnchor))
    contentView.addConstraints(constraints)
}

答案 2 :(得分:0)

可能会对你有所帮助:

class ExampleTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 44
  }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt 
      indexPath: IndexPath) -> CGFloat {
     // optionally, return an calculated estimate for the cell heights
      }

// ...

有关详情,请参阅此处:Link