自定义拆分单元格

时间:2015-09-04 23:59:43

标签: ios uitableview uitextfield uilabel swift2

我正在尝试创建自定义拆分单元格:

my custom splitted cell

顶部包含2个UILabel,底部包含2个自定义UITextField。

我想以编程方式配置单元格左右部分的比例。它应该有时是1:1,有时是3:1。

我在SplittedCellController中尝试了override func layoutSubviews():UITabelViewCell

    override func layoutSubviews() {
    super.layoutSubviews()
    print("cell layout called")
    let leftPart = 2
    let rightPart = 1
    let spacing: CGFloat = 17.0
    let width = self.frame.width
    let allParts = leftPart + rightPart
    let leftPartWidth = (width / CGFloat(allParts)) * CGFloat(leftPart)
    let rightPartWidth = (width / CGFloat(allParts)) * CGFloat(rightPart)

    print(leftPartWidth)
    print(rightPartWidth)


    let leftLabelFrame = leftLabel.frame
    let leftTFFrame = leftTextField.frame
    leftLabel.frame = CGRectMake(leftLabelFrame.origin.x, leftLabelFrame.origin.y, leftPartWidth, leftLabelFrame.size.height)
    leftTextField.frame = CGRectMake(leftTFFrame.origin.x, leftTFFrame.origin.y, leftPartWidth, leftTFFrame.size.height)

    let rightLabelFrame = rightLabel.frame
    let rightTFFrame = rightTextField.frame
    rightLabel.frame = CGRectMake(leftLabelFrame.origin.x + leftPartWidth + spacing, rightLabelFrame.origin.y, rightPartWidth, rightLabelFrame.size.height)

    rightTextField.frame = CGRectMake(leftTFFrame.origin.x + leftPartWidth + spacing, rightTFFrame.origin.y, rightPartWidth, rightTFFrame.size.height)

}

但它没有预期效果: actual effect

在第一部分中,单元格接缝没问题,但是当我点击文本字段时,所需的更改会消失,而且在第二部分中,单元格不会受到影响。

当我点击“已更改”单元格时,右下方文本字段显示在rightView附加标签中,该标签应仅出现在第2部分 - “http://postimg.org/image/8lctbl1ad/”之后的“另一个单元格”中

以下是我用于在textField中显示右视图的代码:

override func drawRect(rect: CGRect) {

    if rightAdditionalLabelShown {
        let label = UILabel(frame: CGRectMake(0, 0, 50, 50))
        label.text = textInRightAdditionalLabel
        label.textColor = UIColor.blackColor()
        label.textAlignment = .Right
        rightView = label
        rightViewMode = .Always
    }


    let border = CALayer()
    let width = CGFloat(0.5)

    border.name = "borderLayer"
    border.borderColor = borderColor.CGColor
    border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: self.frame.size.height)

    border.borderWidth = width
    self.layer.addSublayer(border)
    self.layer.masksToBounds = true
}

我的问题是我应该如何配置单元格以适应所需的比例以及我该如何处理该rightView?

0 个答案:

没有答案