Table cell content(title) moving left after selecting cell

前端 未结 1 1871
一个人的身影
一个人的身影 2021-01-15 02:00

\"enter

In tableview cell, I have set cell title text alignment as center, It is worki

相关标签:
1条回答
  • 2021-01-15 02:14

    It is because of autolayout which you have not set properly. Did you solve your problem. Why do u really need the following

    self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *labelHCN = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0];
    [self.contentView addConstraint:labelHCN];
    NSLayoutConstraint *labelVCN = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0];
    [self.contentView addConstraint:labelVCN];
    

    I thing you have trouble because of this code. Can u try excluding it

    You better declare about the text field as following

    In BaseListCell.m

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
        if (self) {
    
        }
        return self;
    }
    
    
    
    - (void)awakeFromNib {
       self.textLabel.textColor =[UIColor grayColor] 
       self.textLabel.textAlignment = NSTextAlignmentCenter;
       self.textLabel.font = [UIFont mediumFontWithSize:16.f];
      }
    

    First enable the Use Auto layout and Use Size Classes as below do that first in both table view and table view cell .and tell me your problem

    enter image description here

    If you want to place the text vew or label or etc in right of the table view then click on pin of it.enter image description here

    Then select constraint left,right,top and only height. enter image description here

    click on add 4 constraintsenter image description here

    you can check this too:

    iOS - Custom table cell not full width of UITableView

    0 讨论(0)
提交回复
热议问题