我有一个应用程序,其中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分隔符边距?
当前回答
简单地把这两行放在cellForRowAtIndexPath方法中
如果你想要所有的分隔线都从0开始 (细胞setSeparatorInset: UIEdgeInsetsZero); (细胞setLayoutMargins: UIEdgeInsetsZero);
如果你想特定的分隔线是从0开始假设这是最后一行是从0开始
if (indexPath.row == array.count-1)
{
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setLayoutMargins:UIEdgeInsetsZero];
}
else
tblView.separatorInset=UIEdgeInsetsMake(0, 10, 0, 0);
其他回答
至于cdstamper建议的表格视图,在单元格的layoutSubview方法中添加以下行对我来说是可行的。
- (void)layoutSubviews
{
[super layoutSubviews];
if ([self respondsToSelector:@selector(setSeparatorInset:)])
[self setSeparatorInset:UIEdgeInsetsZero];
if ([self respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
{
[self setPreservesSuperviewLayoutMargins:NO];;
}
if ([self respondsToSelector:@selector(setLayoutMargins:)])
{
[self setLayoutMargins:UIEdgeInsetsZero];
}
}
我相信这是我在这里问的相同的问题:为XCode 6 iPhone模拟器删除iOS 8 UITableView上的SeparatorInset
在iOS 8中,所有继承自UIView的对象都有一个新属性。在ios7中设置SeparatorInset的解决方案。x将不能删除你在iOS 8中UITableView上看到的空白。
新的属性叫做“layoutmargin”。
@property(nonatomic) UIEdgeInsets layoutMargins
Description The default spacing to use when laying out content in the view.
Availability iOS (8.0 and later)
Declared In UIView.h
Reference UIView Class Reference
解决方案:
-(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];
}
}
如果你设置单元格。layoutmargin = UIEdgeInsetsZero;如果不检查layoutmargin是否存在,应用程序将在iOS 7.x上崩溃。所以,最好的方法是在setlayoutmargin:UIEdgeInsetsZero之前检查layoutmargin是否存在。
Swift 3.0示例:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// removing seperator inset
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = .zero
}
// prevent the cell from inheriting the tableView's margin settings
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
// explicitly setting cell's layout margins
if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
cell.layoutMargins = .zero
}
}
我的解决方案是添加一个UIView作为单元格子视图的容器。然后设置这个UIView约束(顶部,底部,尾部,前导)为0点。所有不必要的质量都消失了。显示逻辑的图像
-(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;
}
对于要隐藏分隔符的任何特定单元格。