Saf*_*ari 45 iphone interface-builder uitableview ios
我有一个关于UITableView的问题...我有一个UITableViewController,我创建了一个自定义单元格.当我可视化tableView时,我会在分隔线之前看到一个小空格,您可以在此屏幕截图中看到:
为什么?这是默认的可视化?我可以更改一些东西来移除这个白色的左边填充物吗?
Ash*_*hok 89
iOS 7中默认提供前导空格,即使是自定义单元格也是如此.
检查separatorInset
UITableviewCell的此属性以删除/添加单元格行分隔符两端的白色间距.
//删除空格
cell.separatorInset = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)
或者,在UITableView级别,您可以使用此属性 -
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) { // Safety check for below iOS 7
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
Run Code Online (Sandbox Code Playgroud)
-(void)viewDidLayoutSubviews
{
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
-(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];
}
}
Run Code Online (Sandbox Code Playgroud)
mic*_*ila 34
或者,您也可以在界面构建器(IB)中编辑它:
这与@Ashok的答案具有相同的效果,但不需要编写任何代码.
iOS 7和8上的更新工作
iOS 9上的更新工作
Ano*_* VM 16
使用下面的代码删除UITableView中不需要的填充.它适用于IOS 8和7
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*d H 10
没有空白区域!我输入了一个错误,Apple只是把它关闭为"不是一个bug",但告诉我为什么它不是一个bug.我写了一个简单的项目,为应用程序中的每个可能的视图设置颜色.我看到的是那些像素的颜色实际上是细胞本身的背景颜色(不是内容视图!),正如Apple告诉我的那样.
cell.contentView.backgroundColor为绿色,cell.background颜色为红色(与Pixie一起使用):
最后,如果没有分隔符,cell.contentView将完全填充单元格.使用分隔符时,底部有一个或两个像素的间隙.插入时,分隔符填充了大部分间隙,但是细胞中有一些像素显示出来.
谜团已揭开!
编辑:似乎根据您配置故事板的方式,contentView.backgroundColor"丢失"或设置为白色.如果你在提供细胞时过度骑行,你可以得到你想要的行为:
let cell = tableView.dequeueReusableCellWithIdentifier("FOO", forIndexPath: indexPath) as UITableViewCell
cell.backgroundColor = UIColor.redColor()
cell.contentView.backgroundColor = UIColor.greenColor()
Run Code Online (Sandbox Code Playgroud)
cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.frame.size.width, 0)
if (cell.respondsToSelector("preservesSuperviewLayoutMargins")){
cell.layoutMargins = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用
谢谢
这对我有用:
cell?.layoutMargins = UIEdgeInsetsZero;
cell?.preservesSuperviewLayoutMargins = false
Run Code Online (Sandbox Code Playgroud)
对于那些想要在Swift中保留UIEdgeInsetSettings并且不使用Storyboard的人:
之前:(默认行为)
添加以下代码:
tableView.separatorInset.right = tableView.separatorInset.left
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30156 次 |
最近记录: |