Swift: Create a custom UITableviewCell without defining IBOutlet vars - uitableview

I want to create a custom UITableviewCell subclass with RTL UItextlabel text alignment by default. I wrote this small code for that:
class rtlCell : UITableViewCell {
override var textLabel: UILabel {
let label = UILabel()
label.textAlignment = .right
return label
}
}
but this text label doesn't show up in the cell. I know it can be done by defining an IBOutlet label var and connecting a label to it in the storyboard, but I wonder if there is a way to do so without doing these things?
Thanks!

You are creating new label and not adding it to the View hierarchy, your code would work if just styled super.textLabel, e.g. like this:
class MyCell: UITableViewCell
{
override var textLabel: UILabel? {
let label = super.textLabel
label?.textAlignment = .right
return label
}
}
But I think you should move your UI customisation code to some other place (e.g. tableView delegate)

Related

How to disable UITextView area Highlight when clicked inside UITableView in Swift 5?

I have a UITextView Inside a custom UITableview. Whenever I try to click the tableView Cell, the UITextView Bounds gets Highlighted like this. How can I prevent that highlighting? I've tried to disable isUserInteractionEnabled, isEditable and isSelectable inside tableViewCell.swift file but nothing works.
If you want to have an other UIViews in a cell content view to have a the same color as selection and highlight color then use this
class CustomCell: UITableViewCell {
#IBOutlet var textView: UITextView! {
didSet {
textView.backgroundColor = .clear
}
}
}

Why the custom separator view in the cell changes height?

I have custom tableViewCell. And in it cell i have custom separator. The logic of work should be like this:
if select textField in cell - separator change color and
height(from 1 to 2)
if type text - separator color and height not
change
Now it's work like:
if not added constrain for height for separator in .xib - added constrain for height when create cell, but in screen it equal 0
override func awakeFromNib() {
customSeparator.backgroundColor = .lightGray
customSeparator.frame.size.height = 1.0
}
if added constrain for height for separator in .xib - when select cell height of separator change(like expected). But when type text height change to value specified in .xib
func textFieldDidBeginEditing(_ textField: UITextField) {
customSeparator.backgroundColor = .black
customSeparator.frame.size.height = 2.0
}
So, why is this happening, tell me, plz
You need to create an height IBOutlet constraint and change it as follows:
#IBOutlet weak var customSeparatorHeight: NSLayoutConstraint!
override func awakeFromNib() {
customSeparator.backgroundColor = .lightGray
customSeparatorHeight.constant = 1
self.contentView.layoutIfNeeded()
}

Embed Custom UITableViewCell within another custom UIView?

I have a UITableViewController with a list of custom UITableViewCells. The cell has text, which is sometimes long. I am truncating that at 2 lines in the table.
When a user touches the cell, I want to create a UIView as a popup with the exact same information as the UITableViewCell, with the addition of a header above the UITableViewCell content. So, essentially, I want something like the following:
I have built VersePopupView just as indicated in the picture, where the text "Custom Header" is actually a UILabel. I have instantiated the view from the XIB file and successfully set the UILabel text, but the custom UITableViewCell doesn't show, even though I have assigned that value as well. My IBOutlets are all hooked up to IB.
Here is my instantiation:
let popupView: VersePopupView = VersePopupView.instanceFromNib()
popupView.frame = CGRect(x: 10, y: 100, width: 300, height: 100)
popupView.assignVerseTableViewCell(cell: cell)
window.addSubview(popupView)
window.makeKeyAndVisible()
And here is my VersePopupView class:
class VersePopupView: UIView {
#IBOutlet weak var cell: VerseTableViewCell!
#IBOutlet weak var title: UILabel!
#IBOutlet weak var headerView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
class func instanceFromNib() -> VersePopupView {
return UINib(nibName: "VersePopupView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! VersePopupView
}
func assignVerseTableViewCell(cell: VerseTableViewCell)
{
// Assign it
self.cell = cell
// Get the verse details
if let verseDetails = cell.verse_details {
self.title.text = verseDetails.verseReference()
// Adjust the frame
// Adjust the text to be attributed text
// Set the title to the verse reference
// Anything else?
}
}
}
What am I doing wrong such that the UITableViewCell isn't showing up?
Is there a better pattern for this rather than embedding the UITableViewCell?
I don't want to duplicate code (which I have done in the past), because I want the user to interact with the popup just like they would in the table cell.
I'm not sure how to convince you of how easy this is to do without trying to misuse a table view cell, so here's a screen shot:
You just have to believe me when I say that that's a three-row table followed by a totally independent ordinary view plucked from the inside of the table view cell (by loading the cell nib).
They not only look identical, they act identical; the view is a custom UIView subclass with an action method from the switch, and when I click the switch, it triggers that action method, both within the table and in the view outside it.
The view was designed entirely in the nib, and absolutely no code was repeated.
That seems to be the sort of thing you want to do. So, I encourage you, don't misuse table view cells; do this with an ordinary view that can live happily inside a cell or outside it.

Set max lines to 3 and adjust height according to label content

I've created a label in a tableViewCell which contain a string of text. The size of the string can vary however it should maximum be 3 lines. However i can't seem to do this? What is the easiest way to create such a functionality in this label so far i've just created this label in my tableViewCell Subclass.
#IBOutlet weak var dummyLabel: UILabel!
In your custom UITableViewCell class add this:
override func layoutSubviews() {
super.layoutSubviews()
dummyLabel.sizeToFit()
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
dummyLabel.numberOfLines = 3
}
Similar post here about label resize in UITableViewCell: Swift : align UILabel text in the top rather in the middle

Can't change UITableViewCell's subview's position

So I have a static tableview in place and I'd like to change the position of the label inside the first tableviewcell. I have IBOutlet for the first cell.
This is how I try to change the label's position:
UILabel *label1 = [tempTC.cell1.contentView.subviews firstObject];
label1.frame = CGRectMake(10, 10, 10, 10);
The problem is that the frame doesn't change, however I can do everything else to the label like change it's text and size etc, but changing the frame doesn't seem to work.
Is there something I am missing?
When using auto layout, you should set frames, you should change the constraints instead. The easiest way to do this is to make IBOutlets to the ones you need to change, and change the constant value of the constraint. For instance, if you had a constraint to the left side called leftCon, you could do something like this:
self.leftCon.constant = 30;
When you want to change frame subview those were constraint in XIB. You need to set translatesAutoresizingMaskIntoConstraints = YES.
label1.translatesAutoresizingMaskIntoConstraints = YES;
label1.frame = CGRectMake(10, 10, 10, 10);
For detail:
Can I use setFrame and autolayout on the same view?
First of all according to comments, disable auto layout.
Second if You want refer to textLabel use [[UITableViewCell textLabel] setFrame:CGRectMake(10,10,10,10)];
BTW. If You'r first UITablViewCell is special I suggest use .xib and subclass UITableviewCell. Apply that subclass to prototype UITableViewCell in storyboard, then add this cell protype cell as property/global variable.
If you need more description please ask :).
Make a custom UITableViewCell.
Look at the example below.
Inside tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) method I am dynamically changing TimeStatus (UIView) frame size:
.
.
.
let cell:OneTVChannelTableViewCell = tableView.dequeueReusableCellWithIdentifier(OneCurrentChannelTCIdentifier, forIndexPath: indexPath) as OneTVChannelTableViewCell
.
.
.
cell.setForRepairDynamicViewWidth(channelTimeStatusWidth)
.
.
.
And here is my simple custom UITableViewCell:
class OneTVChannelTableViewCell: UITableViewCell {
#IBOutlet weak var Number: UILabel!
#IBOutlet weak var Image: UIImageView!
#IBOutlet weak var Title: UILabel!
#IBOutlet weak var TimeStatus: UIView!
#IBOutlet weak var TimeBack: UIView!
var RepairWidth:CGFloat = 0.0
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func layoutSubviews() {
super.layoutSubviews()
TimeStatus.frame.size.width = RepairWidth
}
func setForRepairDynamicViewWidth(width:CGFloat)
{
RepairWidth = width
}
}

Resources