OGeek|极客世界-中国程序员成长平台

标题: ios - 选择单元格后表格单元格内容(标题)向左移动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:04
标题: ios - 选择单元格后表格单元格内容(标题)向左移动

enter image description here

在 tableview 单元格中,我将单元格标题文本对齐设置为中心,它工作正常,但选择单元格后,标题向左对齐。此问题仅存在于 iOS 7.0 中。

编辑:

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

在 BaseListCell.m 中

- (instancetype)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)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;
}



Best Answer-推荐答案


这是因为您没有正确设置自动布局。 你有没有解决你的问题。 为什么你真的需要以下内容

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];

我认为您会因为这段代码而遇到麻烦。你可以尝试排除它吗

您最好将文本字段声明如下

在 BaseListCell.m 中

- (instancetype)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)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];
  }

首先启用 Use Auto layoutUse Size Classes,如下所示,首先在表格 View 和表格 View 单元格中执行此操作。然后告诉我您的问题

enter image description here

如果您想将 TextView 或标签等放置在表格 View 的右侧,请单击它的图钉。 enter image description here

然后选择约束左、右、上和仅高度。 enter image description here

点击添加 4 个约束 enter image description here

你也可以检查一下:

iOS - Custom table cell not full width of UITableView

关于ios - 选择单元格后表格单元格内容(标题)向左移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206375/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4