簡體   English   中英

帶有UITableViewCellAccessoryDisclosureIndicator的UITableViewCell刪除右邊距

[英]UITableViewCell with UITableViewCellAccessoryDisclosureIndicator remove right margin

我將自動布局UITableViewCell用於iOS 8中引入的動態單元格高度。我設置了單元格,並將accessoryType設置為UITableViewCellAccessoryDisclosureIndicator 我以編程方式進行所有布局。

我嘗試這樣做: self.layoutMargin = UIEdgeInsetsZero; init方法的UITableViewCell內部

在此處輸入圖片說明

我想刪除右邊距或使用contentView調整大小設置自定義值

編輯:添加了用於管理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;
            }
        }
    }
}

更改上面定義的常數以適合您的需求。

希望這可以幫助您解決問題。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM