我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
以上的解决方案我都没有什么运气。我正在为我的表格单元格使用NIB文件。我通过添加一个高度为1的标签来“修复”这个问题。我把标签的背景改为黑色,把标签钉在笔尖的底部,然后把剩下的内容钉在添加的标签上。现在我在单元格的底部有了一条黑色边框。
对我来说,这感觉更像是一种黑客,但它确实有效。
我唯一的选择就是完全消除边界。我还没决定要不要这么做。
其他回答
这在我的iOS 8和iOS 9系统中运行得非常好。
对于OBJ-C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ... Get the cell
cell.separatorInset = UIEdgeInsetsMake(0.f, 20.f, 0.f, [UIScreen mainScreen].bounds.size.width - 20);
// others
return cell;
}
对于要隐藏分隔符的任何特定单元格。
而不是每次单元格滚动时更新preservessuperviewlayoutmargin和layoutmargin(使用willDisplayCell),我建议在cellForRowAtIndexPath::
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
这里有一个简单的方法全局删除嵌入。
在UITableViewCell +移到swift:
import UIKit
extension UITableViewCell {
override public var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set { }
}
}
在AppDelegate应用中:didFinishLaunchingWithOptions
UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero
你可能会想a)也只是覆盖separatorInset在扩展,或b)设置外观代理的layoutmargin,而不是。这两种方法都行不通。即使separatorInset被指示为属性,尝试将其作为属性(或方法)重写也会产生编译器错误。并且为UITableViewCell的layoutmargin设置外观代理(或者,也为UITableView的layoutmargin和separatorInset设置外观代理)没有效果。
使用storyboard更改分隔符插入自定义左0和右0, 如果你想隐藏分隔符为None 见上图