I am trying to create custom splitted cell:
that contains 2 UILabels at top and 2 custom UITextFields at bottom.
And I want programmatically configure the ratio of left and right part of cell. It should be sometimes 1:1 and sometimes 3:1.
I tried override func layoutSubviews()
in my SplittedCellController: 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)
}
But it hasn't desired effect: actual effect
In first section the cell seams to be ok, but when I tapped on a textfield - , desired change disappears and moreover in the second section the cells are not affected.
And when I tap on "changed" cell, the right bottom textfield shows in rightView additional label, that should happen only in section 2 -"http: //postimg.org/image/8lctbl1ad/" after "Another cell"
Here is the code I use for displaying rightview in 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
}
My question is how should I configure the cell to fit desired ratio and what can I do about that rightView?