消除UITableView下面的额外分隔符

当我设置一个包含4行的表视图时,在填充的行下面还有额外的分隔符行(或额外的空白单元格)。

我该如何移除这些细胞?

UITableView中额外分隔线的图像


#1楼

只需添加一个视图,其中所需的分隔符颜色为背景颜色,100%宽度,位于x0 y-1处的1px高度到tableViewCell。 确保tableViewCell不剪辑子视图,而不是tableView应该。

因此,您只能在现有单元格之间获得绝对简单且有效的分隔符,而不会出现每个代码或IB的任何黑客攻击。

注意:在垂直顶部反弹时,第一个分隔符会显示,但这不应该是一个问题,因为它是默认的iOS行为。


#2楼

试试这个。 它对我有用:

- (void) viewDidLoad
{
  [super viewDidLoad];

  // Without ARC
  //self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];

  // With ARC, tried on Xcode 5
  self.tableView.tableFooterView = [UIView new];
}

#3楼

这是另一种方法,可以用分组表格样式,而你可能不会猜到。 向表中添加 页眉和 页脚 (可能是一个或其他,没有检查) 会导致分隔符从填充/空白行中消失。

我偶然发现这是因为我想在桌子的顶部和底部留一点空间,以减少按钮的风险,而不是用手指粗糙的桌子。 这是一种将空白视图作为页眉和页脚粘贴的方法。 使用您喜欢的任何高度,您仍然可以消除额外的分隔线。

- (void) addHeaderAndFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.myTableView setTableHeaderView:v];
    [self.myTableView setTableFooterView:v];
    [v release];
}

为了回应@Casebash,我回到我的应用程序中的代码(iTunes商店中的“AcmeLists”列表管理器......)并将addHeaderAndFooter方法短路以进行验证。 没有它 ,我有额外的行分隔符; 使用代码,我在你的窗口中看到了snap: 没有表格行分隔符图片 。 所以我不确定为什么它不适合你。 此外,对我来说,在表视图上设置任何自定义页脚必然会停止为其下方的空行绘制行分隔符。 那将是可怕的。 作为参考,我查看了可以在屏幕上查看的行数更多的表格,然后查看了包含两行的表格。 在这两种情况下,都没有外来的分离器。

也许您的自定义视图实际上并未添加。 要检查这一点,请将背景颜色设置为clearColor以外的clearColor ,例如[UIColor redColor] 。 如果您在桌子底部没有看到一些红条,则表示您的页脚未设置。


#4楼

我知道这个问题已被接受答案,但我在这里提出了不同的方法来隐藏UITableView额外分隔线。

您可以隐藏tableView的标准分隔线,并在每个单元格的顶部添加自定义行。

更新:

添加自定义分隔符的最简单方法是添加1px高度的简单UIView

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor grayColor]; /// may be here is clearColor;
[cell.contentView addSubview:separatorLineView];

要么

    self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
    self.tblView.delegate=self;
    self.tblView.dataSource=self;
    [self.view addSubview:self.tblView];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.tblView setTableHeaderView:v];
    [self.tblView setTableFooterView:v];
    [v release];

要么

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // This will create a "invisible" footer
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [[UIView new] autorelease];
}

或者我喜欢的最好和最简单的方式是

self.tableView.tableFooterView = [[UIView alloc] init];

尝试任何一个;


#5楼

我使用表视图来显示固定数量的固定高度行,所以我只是调整它的大小并使其不可滚动。


#6楼

适用于使用Storyboard的iOS 7+

只需将UIView作为页脚拖动到UITableView中即可。 将页脚视图的高度设置为0。


#7楼

Swift中的UITableView中删除空行的额外分隔线

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.yourTableview.tableFooterView = UIView()
}

#8楼

推进J. Costa的解决方案:您可以通过以下代码行对表进行全局更改:

[[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

在第一个可能的方法内部(通常在AppDelegate ,在: application:didFinishLaunchingWithOptions:方法中)。


#9楼

试试这个

self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];

#10楼

如果您使用的是Swift,请将以下代码添加到管理tableview的控制器的viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    //...

    // Remove extra separators
    tableView.tableFooterView = UIView(frame: CGRectZero)
}

对于Swift 4.0,不推荐使用CGRectZero,因此您必须使用CGRect.zero


#11楼

对于Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
    }

#12楼

我很幸运地实现了一个接受的答案(iOS 9 +,Swift 2.2)。 我曾尝试过实施:

self.tableView.tableFooterView = UIView(frame: .zero)

但是,我的tableView没有任何影响 - 我相信它可能与我使用UITableViewController的事实有关。

相反,我只需要覆盖viewForFooterInSection方法(我没有在其他地方设置tableFooterView ):

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView(frame: .zero)
}

这适用于具有单个部分的tableView (如果您有多个部分,则需要指定最后一个部分)。


#13楼

如果你有一个searchbar在你的view (限制的结果,例如数量),你必须也添加以下shouldReloadTableForSearchStringshouldReloadTableForSearchScope:

controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];

#14楼

uitableview额外的分隔线隐藏额外的分隔符行隐藏在swift 3.0中

 self.tbltableView.tableFooterView = UIView(frame: .zero)

#15楼

Swift非常适合:

tableView.tableFooterView = UIView()

#16楼

如果您不希望在最后一个单元格后面有任何分隔符,那么您的页脚需要接近零但非零高度。

在你的UITableViewDelegate

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}

#17楼

你可以在最后添加一个空页脚然后它会隐藏空单元格,但它看起来也很难看:

tableView.tableFooterView = UIView()

在此输入图像描述

有一种更好的方法:在表格视图的末尾添加1点线作为页脚,空单元格也将不再显示。

let footerView = UIView()
footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1)
footerView.backgroundColor = tableView.separatorColor
tableView.tableFooterView = footerView

在此输入图像描述


#18楼

如果要在UITableview中删除不需要的空间,可以使用以下两种方法

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

#19楼

要以编程方式从UItableview底部消除额外的分隔线,只需记下以下两行代码,它将从中删除额外的分隔符。

tableView.sectionFooterHeight = 0.f;
tableView.sectionHeaderHeight = 0.f;

这个技巧一直在为我工作,试试自己。


#20楼

只需添加此代码( Swift )。 。

tableView.tableFooterView = UIView()

#21楼

如果您只有一个部分,那么最快捷最简单的方法是将表视图样式从“普通”设置为“分组”。 (见图)

TableView分组

如果您有更多部分,则可能需要将标题高度设置为零(取决于您/您的客户/项目经理的口味)

如果你有更多的部分,并且不想弄乱标题(即使在最简单的情况下它只是一行),那么你需要将UIView设置为页脚,就像之前的答案中所解释的那样)


#22楼

Swift 4.0扩展

只是故事板的一个小扩展:

在此输入图像描述

extension UITableView {
    @IBInspectable
    var hideSeparatorForEmptyCells: Bool {
        set {
            tableFooterView = newValue ? UIView() : nil
        }
        get {
            return tableFooterView == nil
        }
    }
}

#23楼

tableView具有tableFooterView时, UIKit不会创建空单元格。 所以我们可以制作一个技巧,并将零高度的UIView对象指定为tableView页脚。

tableView.tableFooterView = UIView()

#24楼

快速简便的Swift 4方式。

override func viewDidLoad() {
     tableView.tableFooterView = UIView(frame: .zero)
}

如果你有静电电池。 您还可以从“检查器”窗口关闭分隔符。 (如果你需要分隔符,这是不可取的。在这种情况下,使用上面显示的方法) 检查员窗口


#25楼

在Swift(我使用4.0)中,您可以通过创建自定义UITableViewCell类并overriding setSelected方法来实现此目的。 然后separator insets全部separator insets0 。 (我的主要类与表视图有明确的背景)颜色。

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // eliminate extra separators below UITableView
    self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

#26楼

我添加了这个小的tableview扩展,它可以帮助你

extension UITableView {
     func removeExtraCells() {
         tableFooterView = UIView(frame: .zero)
     }
}

#27楼

你可能会找到很多这个问题的答案。 其中大多数都是使用UITableViewtableFooterView属性进行操作,这是隐藏空行的正确方法。 为方便起见,我创建了简单的扩展,允许从Interface Builder打开/关闭空行。 你可以从这个gist文件中查看它。 我希望它可以节省你的一点时间。

extension UITableView {

  @IBInspectable
  var isEmptyRowsHidden: Bool {
        get {
          return tableFooterView != nil
        }
        set {
          if newValue {
              tableFooterView = UIView(frame: .zero)
          } else {
              tableFooterView = nil
          }
       }
    }
}

用法:

tableView.isEmptyRowsHidden = true

在此输入图像描述


#28楼

界面构建器(iOS 9+)

只需将UIView拖到表中即可。 在故事板中,它将位于自定义单元格下方的顶部。 您可能更喜欢将其命名为“页脚”。

为清晰起见,此处以绿色显示,您可能需要清晰的颜色。

请注意,通过调整高度,您可以根据需要影响表格的“底部反弹”处理方式。 (高度零通常很好)。

在此输入图像描述


要以编程方式执行此操作:

迅速

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.tableFooterView = UIView()
}

Objective-C的

iOS 6.1+

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [UIView new];
}

或者如果你愿意,

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

历史上在iOS中:

添加到表视图控制器...

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     // This will create a "invisible" footer
     return CGFLOAT_MIN;
 }

如有必要......

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{        
    return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}

#29楼

试试这个

目标C

- (void)viewDidLoad 
 {
  [super viewDidLoad];
  // This will remove extra separators from tableview
  self.yourTableView.tableFooterView = [UIView new];
}

斯威夫特来说

override func viewDidLoad() {
 super.viewDidLoad()
 self.yourTableView.tableFooterView = UIView()
}

#30楼

我想延长wkw的回答:

只需添加高度为0的页脚就可以了。 (在sdk 4.2,4.4.1上测试)

- (void) addFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];

    [self.myTableView setTableFooterView:v];
}

甚至更简单 - 在您设置tableview的地方,添加以下行:

//change height value if extra space is needed at the bottom.
[_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];

甚至更简单 - 只需删除任何分隔符:

[_tableView setTableFooterView:[UIView new]];

再次感谢wkw :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值