如何确定分组UITableView的边距(或者更好,如何设置它)?

use*_*814 32 uitableview

分组的UITableView在视图边缘和表格单元格之间放置一个边距.令人讨厌(对我来说)这个边距是视图宽度的一些功能.

在我的应用程序中,我有两个不同宽度的UITableViews,我希望对齐细胞边缘.

有可能获得这个保证金吗?或者更好的是可以设置这个保证金?

欢呼, - 本

Mat*_*mas 53

与细胞边距相关的分组TableView宽度

TBWidth(0到20)左边距= TBWidth - 10

TBWidth(20到400)左边距= 10

TBWidth(401到546)左边距= 31

TBWidth(547到716)左边距= apx 6%的tableView

TBWidth(717到1024)左边距= 45

- (float) groupedCellMarginWithTableWidth:(float)tableViewWidth
{
    float marginWidth;
    if(tableViewWidth > 20)
    {
        if(tableViewWidth < 400)
        {
            marginWidth = 10;
        }
        else
        {
            marginWidth = MAX(31, MIN(45, tableViewWidth*0.06));
        }
    }
    else
    {
        marginWidth = tableViewWidth - 10;
    }
    return marginWidth;
}
Run Code Online (Sandbox Code Playgroud)

  • 对于iPhone,当设备方向是横向时,边距仍为10.因此上面代码的第6行应检查设备是否也是iPhone.在Monotouch中,这是它的工作原理:if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone || tableViewWidth <400) (4认同)

Ste*_*mer 18

通过创建(和使用!)UITableViewCell的子类,您可以实现您正在寻找的东西.只需在layoutsubviews中移动contentview和backgroundview,就像下面的代码示例一样,将单元格20pt的可见部分向右移动.

- (void) layoutSubviews
{
    NSLog (@"Cell/contentview/backgroundview before:\n%@\n%@\n%@", self, self.contentView, self.backgroundView);
    [super layoutSubviews];
    CGRect frame = self.backgroundView.frame;
    frame.origin.x += 20;
    frame.size.width -= 20;
    self.backgroundView.frame = frame;
    frame = self.contentView.frame;
    frame.origin.x += 20;
    frame.size.width -= 20;
    self.contentView.frame = frame;

    NSLog (@"Cell/contentview/backgroundview after:\n%@\n%@\n%@", self, self.contentView, self.backgroundView);
}
Run Code Online (Sandbox Code Playgroud)

  • 同意,然而这个解决方案看起来很脆弱,取决于内部细节! (3认同)
  • -1 Matthew Thomas的回答使用内置边距. (2认同)

dav*_*ode 12

我正在使用Matthew Thomas实现,但它不可靠(如果你在你的控制器中使用自动旋转它就坏了)并且有很多硬编码代码......我意识到有一个非常简单的解决方案,我的实现是一行代码:

- (float)cellMargins
{
    return self.backgroundView.frame.origin.x * 2;
}
Run Code Online (Sandbox Code Playgroud)

这是更好的IMO :)

编辑:我的实现也不可靠(切换到编辑模式时无法正常工作),这是我的最终实现(我分别处理左右边距):

- (float)leftMargin
{
    return self.contentView.frame.origin.x;
}

- (float)rightMargin
{
    CGRect frame = self.contentView.frame;
    float containerWidth = frame.size.width;
    float margin = self.frame.size.width - (containerWidth + frame.origin.x);
    return margin;
}

- (float)cellMargins
{
    return ([self leftMargin] + [self rightMargin]);
}
Run Code Online (Sandbox Code Playgroud)

  • 您的解决方案看起来很棒,但在创建单元格之前需要已知边距的情况下无法使用 (5认同)

tt.*_*lew 6

更新了Matthew Thomas的细胞边缘计算代码.通过UITableView的类别创建.当您需要确定单元格中的文本高度时,这可能很有用,并且您不知道该单元格的宽度.

因此,可以像计算实际单元格一样

cell.width = tableView.width - tableView.cellsMargin * 2;
Run Code Online (Sandbox Code Playgroud)

这是代码

@implementation UITableView (CellsMargins)

// This is black magic
// from
// http://stackoverflow.com/questions/4708085/how-to-determine-margin-of-a-grouped-uitableview-or-better-how-to-set-it

- (CGFloat)cellsMargin {

   // No margins for plain table views
   if (self.style == UITableViewStylePlain) {
      return 0;
   }

   // iPhone always have 10 pixels margin
   if (! isIPad()) {
      return 10;
   }

   CGFloat tableWidth = self.frame.size.width;

   // Really small table
   if (tableWidth <= 20) {
      return tableWidth - 10;
   }

   // Average table size
   if (tableWidth < 400) {
      return 10;
   }

   // Big tables have complex margin's logic
   // Around 6% of table width,
   // 31 <= tableWidth * 0.06 <= 45

   CGFloat marginWidth  = tableWidth * 0.06;
   marginWidth = MAX(31, MIN(45, marginWidth));
   return marginWidth;
}

@end
Run Code Online (Sandbox Code Playgroud)