【问题标题】:UITableViewCell with UITableViewCellAccessoryDisclosureIndicator remove right margin带有 UITableViewCellAccessoryDisclosureIndicator 的 UITableViewCell 删除右边距
【发布时间】:2015-10-29 07:29:13
【问题描述】:

我使用自动布局UITableViewCell 来实现iOS 8 中引入的动态单元格高度。 我设置了我的单元并将accessoryType 设置为UITableViewCellAccessoryDisclosureIndicator。我以编程方式进行所有布局。

我试过这样做: self.layoutMargin = UIEdgeInsetsZero;UITableViewCellinit 方法中

我想通过 contentView 调整大小来删除右边距或设置自定义值

【问题讨论】:

  • 看看这个帖子里的答案:stackoverflow.com/questions/26755451/…
  • @GurtejSingh 我尝试了这个解决方案,但它不起作用,因为我以编程方式使用 PureLayout 进行布局。
  • 请在下面查看我的答案,如果它有效,请告诉我。谢谢。
  • 在下面更新了我的答案。请看看它现在是否有效。
  • 嘿,我的回答对你有用吗?如果是,那么您应该接受它(单击我回答中投票按钮下方的复选标记)如果您仍有任何问题,请告诉我。

标签: ios uitableview ios8 autolayout


【解决方案1】:

编辑:添加了用于管理 textLabel 和 detailTextLabel 框架的代码。

您可以通过在您的自定义单元格类中覆盖layoutSubViews 方法来实现这一点(如果您不使用一个,那么首先创建一个并在您的表格视图中使用它)。将以下代码添加到您的表格视图单元类 .m 文件中:

const int ACCESORY_MARGIN = -10;
const int LABEL_MARGIN = -10;

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame;
    frame = self.textLabel.frame;
    frame.origin.x += LABEL_MARGIN;
    frame.size.width -= 2 * LABEL_MARGIN;
    self.textLabel.frame = frame;

    frame = self.detailTextLabel.frame;
    frame.origin.x += LABEL_MARGIN;
    frame.size.width -= 2 * LABEL_MARGIN;
    self.detailTextLabel.frame = frame;

    if (self.accessoryType != UITableViewCellAccessoryNone)
    {
        float estimatedAccesoryX = MAX(self.textLabel.frame.origin.x + self.textLabel.frame.size.width, self.detailTextLabel.frame.origin.x + self.detailTextLabel.frame.size.width);

        for (UIView *subview in self.subviews) {
            if (subview != self.textLabel &&
                subview != self.detailTextLabel &&
                subview != self.backgroundView &&
                subview != self.contentView &&
                subview != self.selectedBackgroundView &&
                subview != self.imageView &&
                subview.frame.origin.x > estimatedAccesoryX) {
                frame = subview.frame;
                frame.origin.x -= ACCESORY_MARGIN;
                subview.frame = frame;
                break;
            }
        }
    }
}

更改上面定义的常量以满足您的需要。

希望这有助于并解决您的问题。谢谢。

【讨论】:

  • 抱歉,我创建了一个测试项目并添加了您的代码。结果是下一个,附件向右移动,标签保持与以前相同的宽度和位置
  • 好吧,代码按预期工作。您从未在问题中提到您也想移动标签。请相应地更新您的问题,我将更新答案。谢谢
猜你喜欢
  • 2016-12-07
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多