簡體   English   中英

如何在 UITableView 中設置分隔符的全寬

[英]How to set the full width of separator in UITableView

我有一個UITableView ,其中分隔符沒有全寬。 它在左側前 10 個像素處結束。 我在viewDidLoad()玩弄這段代碼。

self.tableView.layoutMargins = UIEdgeInsetsZero;

同樣在故事板中,您可以選擇自定義或默認選擇器。 現在填充的所有單元格都沒有全寬選擇器,但空的單元格具有全寬。

我怎樣才能解決這個問題?

這在使用 Xcode 6.4 和 Swift 1.2 的 iOS 8.4 - 9.0 設備上對我有用:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()


    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero

    return cell
}

斯威夫特 5 更新:

cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero

我從這篇文章中得到了答案: iOS 8 UITableView separator inset 0 not working

只需在您的UITableViewController上添加此代碼

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

在你的UITableViewCell

轉到界面生成器中的 Attributes Inspector 並將“15”更改為 0。對您希望更改的所有單元格執行此操作。

插入

您可能需要添加[cell setLayoutMargins:UIEdgeInsetsZero]; 到您的tableViewCell

對於 Swift 3 :

override func viewDidLoad() {
  super.viewDidLoad()

  tableView.separatorInset = .zero
  tableView.layoutMargins = .zero
}
  1. 選擇你的UITableViewCell
  2. 轉到屬性檢查器
  3. 轉到分隔符並將其更改為“自定義插入”
  4. 設置left和/或right字段。 (默認left: 15right: 0

看看它是如何工作的(使用left: 100 ):

在此處輸入圖片說明

結果:

在此處輸入圖片說明

我從UITableViewController繼承,並且除了willDisplayCell的兩個 insets 設置willDisplayCell還需要將preservesSuperviewLayoutMargins設置為 false。 在 Swift 中,它看起來像這樣:

override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }
}

分隔符Inset默認為從左側開始 15。 將 Separator Inset選項從auto更改為custom並將 inset 設置為0

在此處輸入圖片說明

適用於 iOS 9+ 中的 Swift

如果使用自定義UITableViewCell

override var layoutMargins: UIEdgeInsets {
    get { return UIEdgeInsetsZero }
    set(newVal) {}
}

然后對於viewDidLoadUITableView

self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;

cellForRowAtIndexPath方法中使用它,配置單元格的分隔符規格,
它適用於 iOS9.0+

 cell.separatorInset = UIEdgeInsetsZero;
 cell.layoutMargins = UIEdgeInsetsZero;
 cell.preservesSuperviewLayoutMargins = NO;

對於 iPad 有問題的人——這會讓你處於與 iPhone 相同的狀態。 然后,您可以根據需要調整separatorInset

tableView.cellLayoutMarginsFollowReadableWidth = false

已針對 iOS 9.3 和 Swift 2.2 進行測試。 確保將代碼放在willDisplayCell ,該代碼在顯示單元格之前被調用,而不是放在僅創建單元格的cellForRowAtIndexPath中。

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero
}

UITableViewController的函數添加override ,如下所示:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

快速 5,Xcode 11 更新

把它放在 viewDidLoad() 里面

yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

對於邊到邊分隔符,只需將 left 和 right 的值設置為 0。

順便將“yourTableView”更改為您的tableView Outlet 名稱。

以上在 Swift 2.2 和 Xcode 7.3.1 中都不適合我

結果證明這是最簡單的解決方案。 不需要代碼。 只需在UITableView檢查器中更改TableViewCell Layout Margins 值:

對於 Swift 3 :

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = UIEdgeInsets.zero
    }
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
    if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
}

viewDidLoad (測試 iOS11 - swift 4.1)

嘗試

tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)

這些解決方案都不適用於 iPad,但我提出了一個涵蓋兩種設備的解決方案:

使用可重復使用的細胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    ...[other code]...
    [cell setLayoutMargins:UIEdgeInsetsZero];
    [cell setSeparatorInset:UIEdgeInsetsZero];
    return cell;
}

對於不可重復使用的單元格:

- (void)removeSeparatorInset:(UITableView*)tableView{
    NSArray *cells = [tableView visibleCells];
    for (UITableViewCell *cell in cells){
        [cell setLayoutMargins:UIEdgeInsetsZero];
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

-(void) viewDidLayoutSubviews{
   [super viewDidLayoutSubviews];
   [self removeSeparatorInset:self.tableView];
}

只是為了擴展這種方法:

@property(nonatomic) UIEdgeInsets separatorInset;
@property(nonatomic) UIEdgeInsets layoutMargins;

UITableViewUITableViewCell可以使用這兩個屬性。 后者實際上是UIView一個屬性,它是UITableViewUITableViewCell的父類。

暫無
暫無

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

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