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

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


当前回答

在iOS9上,我有一个问题,改变分隔符插入也会影响文本和细节标签的定位。

我用这个解出来了

override func layoutSubviews() {
    super.layoutSubviews()

    separatorInset = UIEdgeInsets(top: 0, left: layoutMargins.left, bottom: 0, right: width - layoutMargins.left)
}

其他回答

  if([_data count] == 0 ){
       [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//  [self tableView].=YES;
    } else {
      [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];////    [self tableView].hidden=NO;
    }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

       NSString *cellId = @"cell";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
       NSInteger lastRowIndexInSection = [tableView numberOfRowsInSection:indexPath.section] - 1;

       if (row == lastRowIndexInSection) {
              CGFloat halfWidthOfCell = cell.frame.size.width / 2;
              cell.separatorInset = UIEdgeInsetsMake(0, halfWidthOfCell, 0, halfWidthOfCell);
       }
}

你必须采取自定义单元格,并添加标签和设置约束,如标签应覆盖整个单元格区域。 并在构造函数中编写下面的行。

- (void)awakeFromNib {
    // Initialization code
    self.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
    //self.layoutMargins = UIEdgeInsetsZero;
    [self setBackgroundColor:[UIColor clearColor]];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
}

也设置UITableView布局边距如下

tblSignup.layoutMargins = UIEdgeInsetsZero;

来跟进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()
}

更简单、更符合逻辑的做法是:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectZero];
}

在大多数情况下,您不希望只看到最后一个表视图单元格分隔符。这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符嵌入。