UITextField in custom UITableViewCell not showing

I create a custom UITableViewCell (programmatically, by subclassing) with one label and one text field.

This code

#import "TextCell.h" @implementation TextCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { UITextField *subjectField = [[UITextField alloc] initWithFrame:CGRectMake(59, 11, 399, 21)]; subjectField.font = [UIFont systemFontOfSize:17]; subjectField.placeholder = @"(placeholder)"; [self.contentView addSubview:subjectField]; self.textField = subjectField; } return self; } 

leads to an invisible text field:

screenshot of the invisible field

But if I select this cell, the text box will become visible:

enter image description here

If I implement

 - (void)layoutSubviews { self.textField.frame = CGRectMake(59, 11, 399, 21); /* thus text field init method becomes initWithFrame:CGRectZero; } 

the text field becomes visible, but the label goes away, and the cell width increases dramatically:

enter image description here

Please call me in the right direction.

+4
source share
1 answer

On the first attempt, the text field will be behind the label and therefore will not be visible. I suppose the standard label from the cell you are using? It is not created until you get it, and by default it is the full width of the cell, so it covers your text box. To avoid this, use your own label with a special frame.

In the second example, you forgot to call [super layoutSubviews] , which is very important.

+5
source

Source: https://habr.com/ru/post/1435626/


All Articles