• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 选择单元格后表格单元格内容(标题)向左移动

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:04:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap