分组的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)
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)
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)
更新了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)
| 归档时间: |
|
| 查看次数: |
17403 次 |
| 最近记录: |