Table cell contents (title) moving left after cell selection

enter image description here

In the tableview cell, I set the alignment of the text of the cell title as the center, it works fine, but after selecting the cell, the title alignment to the left. This issue only applies to iOS 7.0.

Edit:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BaseListCell *cell = [self.listTableView dequeueReusableCellWithIdentifier:listCellIdentifier forIndexPath:indexPath]; cell.textLabel.text = _listArray[indexPath.row]; cell.textLabel.userInteractionEnabled = NO; return cell; } 

In BaseListCell.m

 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; if (self) { self.textLabel.textColor =[UIColor grayColor]; self.textLabel.textAlignment = NSTextAlignmentCenter; self.textLabel.font = [UIFont mediumFontWithSize:16.f]; 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]; } return self; } 
+2
ios iphone
source share
1 answer

This is due to autorun, which you did not install properly. You have solved your problem. Why do you 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 have problems because of this code. You can try excluding it.

It is better to declare a text field as follows

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 Use automatic layout and Use dimension classes , as shown below, as shown below, both in a table and in a table form. And tell me your problem

enter image description here

If you want to put vew text or shortcut, etc. To the right of the table view, click on its icon. enter image description here

Then select the restriction on the left, right, top and only height. enter image description here

click to add 4 restrictions enter image description here

You can also check this:

iOS - Custom table cell not full width UITableView

+1
source share

All Articles