我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?

我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。


当前回答

对于iOS7或更高版本,更简洁的方法是使用INFINITY而不是硬编码的值。当屏幕旋转时,您不必担心更新单元格。

if (indexPath.row == <row number>) {
    cell.separatorInset = UIEdgeInsetsMake(0, INFINITY, 0, 0);
}

其他回答

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
in viewDidLoad() {    

   tableView.separatorStyle = UITableViewCellSeparatorStyle.None 

}

正如(许多)其他人指出的那样,你可以通过简单地关闭整个UITableView本身来轻松隐藏所有UITableViewCell分隔符;例如在你的UITableViewController中

- (void)viewDidLoad {
    ...
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    ...
}

不幸的是,在每个单元格的基础上,这是一个真正的PITA,这是你真正要求的。

就我个人而言,我已经尝试了许多改变cell.separatorInset的排列。正如(许多)其他人所建议的那样,再次向左,但问题是,引用苹果的话(强调):

"...您可以使用此属性在当前单元格内容与表的左右边缘之间添加空格。正插入值将单元格内容和单元格分隔符向内移动,远离表格边缘……”

因此,如果你试图通过将分隔符推到屏幕的右侧来“隐藏”分隔符,你最终也会缩进你的单元格的contentView。正如crifan所建议的,您可以尝试通过设置cell来补偿这种讨厌的副作用。indentationWidth和cell。indentationLevel适当地将所有内容移回,但我发现这也是不可靠的(内容仍然缩进…)

我发现的最可靠的方法是在一个简单的UITableViewCell子类中覆盖layoutSubviews,并设置右插入,使它命中左插入,使分隔符有0宽度,因此不可见[这需要在layoutSubviews中自动处理旋转]。我还向我的子类添加了一个方便的方法来打开它。

@interface MyTableViewCellSubclass()
@property BOOL separatorIsHidden;
@end

@implementation MyTableViewCellSubclass

- (void)hideSeparator
{
    _separatorIsHidden = YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (_separatorIsHidden) {
        UIEdgeInsets inset = self.separatorInset;
        inset.right = self.bounds.size.width - inset.left;
        self.separatorInset = inset;
    }
}

@end

警告:没有一个可靠的方法来恢复原始的右插入,所以你不能“取消隐藏”分隔符,因此我为什么要使用一个不可逆的hideSeparator方法(vs暴露separatorIsHidden)。请注意,separatorInset在重用的单元格之间持续存在,因为你不能“取消隐藏”,你需要将这些隐藏的分隔单元格隔离在它们自己的reuseIdentifier中。

来跟进Hiren的回答。

在ViewDidLoad和下面一行:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

或者,如果你正在使用XIB或storyboard,将“separator”更改为“none”:

在CellForRowAtIndexPath中添加这个:

CGFloat separatorInset; // Separator x position 
CGFloat separatorHeight; 
CGFloat separatorWidth; 
CGFloat separatorY; 
UIImageView *separator;
UIColor *separatorBGColor;

separatorY      = cell.frame.size.height;
separatorHeight = (1.0 / [UIScreen mainScreen].scale);  // This assures you to have a 1px line height whatever the screen resolution
separatorWidth  = cell.frame.size.width;
separatorInset  = 15.0f;
separatorBGColor  = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];

separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
separator.backgroundColor = separatorBGColor;
[cell addSubView: separator];

这是一个结果的例子,我显示一个动态单元格的tableview(但只有一个内容)。结果是,只有一个有分隔符,而不是所有的“虚拟”tableview自动添加填充屏幕。

编辑:对于那些不经常阅读注释的人来说,实际上有一种更好的方法,只需几行代码:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.tableFooterView = UIView()
}
  if([_data count] == 0 ){
       [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//  [self tableView].=YES;
    } else {
      [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];////    [self tableView].hidden=NO;
    }