我有一个应用程序,其中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分隔符边距?
当前回答
这就是我的解。这适用于自定义单元格子类,只需将它们都添加到子类。
- (UIEdgeInset)layoutMargins { 返回 UIEdgeInsetsMake(0, 10, 0, 10); }
2.
self.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
而且很方便,你可以自定义分隔符的位置,而不需要你的设计师为你画一个..........
其他回答
这在我的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;
}
Arg ! !在你的Cell子类中做这些事情后:
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
或者设置单元格。layoutmargin = UIEdgeInsetsZero;帮我修好了。
我的解决方案是添加一个UIView作为单元格子视图的容器。然后设置这个UIView约束(顶部,底部,尾部,前导)为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];
}
}
使用Swift 2.2
创建UITableViewCell扩展
import UIKit
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector(Selector("setSeparatorInset:")) {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector(Selector("setLayoutMargins:")) {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
现在可以在cellForRowAtIndex中使用
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.removeMargins()//To remove seprator inset
}