问题描述
在tableview中单元格,我将单元格标题文本对齐设置为中心,工作正常,但选择单元格后,标题左对齐。此问题仅适用于iOS 7.0。
In tableview cell, I have set cell title text alignment as center, It is working fine but after selecting cell, title aligning left. this issue is only in iOS 7.0.
修改:
- (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;
}
在BaseListCell.m
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;
}
推荐答案
这是因为autolayout你没有正确设置。
你解决了问题吗?
为什么你真的需要以下
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
在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
如果要放置文本vew或标签等在表格视图右侧,然后点击它的图钉。
If you want to place the text vew or label or etc in right of the table view then click on pin of it.
然后选择约束left,right,top和only height。
Then select constraint left,right,top and only height.
点击添加4个约束
您也可以查看:
这篇关于选择单元格后,表格单元格内容(标题)向左移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!